While as an expression

Haxe is said to follow everything is an expression, which is true with if, try, blocks, and list comprehensations, this is very good to reduce the chance of not initializing a varibale in some condition.

But it seems not to work with do/for/while family, where I would expect it to return the last expression of the last iteration, or an array of the last expressions (as in coffeescript)
Example the following

var my_var=if(highmode){
"genesis";
} else if (lowmode){
try {
"success";
} catch(ex){
while(true){
trace("all failed");
var test=Sys.stdin().readLine();
if (verfied(test){
test;
break;
}
}
}
}

another option could be to decalre an anynimus recursive function, and return the call in last line, but do not look so pretty.

var my_var=if(highmode){
"genesis";
} else if (lowmode){
try {
"success";
} catch(ex){
function getval(){
trace("all failed");
var test=Sys.stdin().readLine();
if (verfied(test){
test;
} else {
getval();
}//if
}//function
getval();
}//try
}//top-if

Is this by design? are any other options availible?

If you do want all expressions, you can use a comprehension.

For just getting the last evaluated expression, it’s a limitation that probably isn’t that easy to overcome. You can propose a solution over at GitHub - HaxeFoundation/haxe-evolution: Repository for maintaining proposal for changes to the Haxe programming language, but you should bare in mind that chances of it being accepted are much higher if it doesn’t break anything.

Consider for example the following:

var a = [];
while (something)
  if (condition) a.push('trololo');
  else if (otherCondition) a.pop();

One branch returns Int, the next String and the next Void. This can probably be resolved by taking into account whether a return value is actually expected, but well, I’ll let you brood over that, if you feel strongly about having this feature :wink:

Thanks for your example, this showed me 2 things.

#1 Certainly much code will be broken if we make this change, the fix is obvious but the benefit doesn’t justify the hassle, I am having this issue many times when translating JS code to Haxe and a function has a If at the tale, with a some discard returns, e. .g Array.push in a branch.

#2 My temptation to declare it only once came from translating a buggy piece of javascript that was using a var here and there, and obviously some conditions where not handled, causing a “torolo is not defined” at runtime some point later in the code.
Seeing your code, gave me the idea that I could use a block with a limited scope to declare the variable before the loop, without giving a chance to pollute the parent scope.

{
var a;
while(true){
	a=readline();
	if (verified(a)) break;
	}
	a;

}