class Test {
static function main() {
var s = "5";
var v = null;
if( ( v = Std.parseInt( s ) ) != null )
trace( "ok", v );
else
trace( "not ok", v );
}
}
I know all in haxe is expression and here it’s not really a big deal, but having many if/else force you to declare the local variable all on the top of all if/else blocks.
Yes I also use this kind of pattern matching in other cases, here my example is not very good because i’ve simplified it too much.
But ok, if there is no other way, there isn’t
Here is maybe a better example what I wanted to shortcut :
class Test {
static function main() {
var sval = "5";
var val : Dynamic = sval;
if( sval == "true" || sval == "false" )
val = sval == "true";
else{
var v = null;
if( ( v = Std.parseInt( sval ) ) != null ){
val = v;
}
}
trace( val );
}
}
Just to have less lines to write :
class Test {
static function main() {
var sval = "5";
var val : Dynamic = sval;
if( sval == "true" || sval == "false" ){
val = sval == "true";
}else if( ( var v = Std.parseInt( sval ) ) != null )
val = v;
}
trace( val );
}
}