mardi 28 novembre 2017

Naming a condition if-value for use in if block

To limit the amount of double code instances i would like to name my variables in if statement much like you do in for loops.

My expression:

var hours = if (this.substringBefore(":").toInt() != 0) 
   {this.substringBefore(":") + "h" }
   {else ""}

I want something like:

var hours = if (MY_VAR = this.substringBefore(":").toInt() != 0) 
   { MY_VAR + "h" }
   else { "" }

I mainly write in kotlin, but I'm interested in finding other languages that does this.

Aucun commentaire:

Enregistrer un commentaire