Open LPeter1997 opened 2 years ago
In the seqential return value case we should just allow the result to implement IEnumerable
and then every time it loops it would do everything and then it would add the last expresion to the collection which will be returned
E. g.
var array=for(var i=0; i<5; i++) {
println("a"); //prints a every time before it eveluates i*i
i*i
}
It came up in a discussion that since most control structures will return a value, we could also define some sensible evaluation value for loops too. There are two semantics that are sensible enough to consider (at least the ones we could think of) + the usual, safe defaults:
Singular return value
This would be what Rust does. The loop evaluates to a single value. In Rust this is only valid in the
loop
construct (infinite loop) and everybreak
has an associated return value. Here is an example.Sequential return value
This would essentially be implicit generators. Something like this:
Could print
0, 1, 4, 9, 16
.This brings up a few questions, for example will this make the loop essentially lazy? What will the following piece of code do?
Will this immediately print the 5
a
s or only whensquares
is enumerated? Wouldn't this behavior be too misleading?Evaluate to unit
The simplest semantic would be that all loops simply evaluate to unit. This means they can still be used where expressions can (like in arrow-bodied methods), but their result is essentially meaningless.