break and continue statements
betseg opened this issue ยท comments
โญ๏ธ Proposed change
Adding โน
for break
ing from a loop (maybe with a value like Rust does?) and โญ
for continue
ing a loop. This may be even improved with labelled loops, tho its syntax would be harder to be agreed upon.
๐ค Rationale
A useful feature. I chose โญ because Unicode defines it as "next track", and โน is "stop".
๐บExample
"Standard" break
:
0 โก๏ธ ๐๐a
๐ ๐ ๐
a โฌ
๏ธโ 1
โช๏ธ a โถ๏ธ 5 ๐
โน
๐
๐
break
ing with a value:
0 โก๏ธ ๐๐a
๐ ๐ ๐
a โฌ
๏ธโ 1
โช๏ธ a โถ๏ธ 5 ๐
โน a โ๏ธ
๐
๐ โก๏ธ thisVarIsSix
continue
:
0 โก๏ธ ๐๐sum
๐ i ๐โฉโฉ 0 10โ๏ธ ๐
โช๏ธ i ๐ฎ 2 ๐ 1 ๐
โญ
๐
๐
๐
sum โฌ
๏ธโ i
๐ ๐ก sum 10 โ๏ธโ๏ธ
๐
๐
I havenโt heard of "breaking with a value" before. The same behavior could be easily achieved with a method, couldnโt it?
Yeah, it can be.
โน is nice. โญ conflicts with https://www.emojicode.org/docs/packages/s/23e9.html#iโญ though.
I donโt think that "breaking with a value" is something that fits Emojicode particularly well.
And what about breaking out of/continuing nested loops? Some languages have labels for that purpose. Example from Rust:
fn main() {
'outer: loop {
println!("Entered the outer loop");
'inner: loop {
println!("Entered the inner loop");
// This would break only the inner loop:
//break;
// This breaks the outer loop:
break 'outer;
}
println!("This point will never be reached");
}
println!("Exited the outer loop");
}
Actually, I have rarely seen breaking to a specific loop being used in real production code, so I doubt it is something people will want to do in Emojicode. Moreover, it adds complexity to the reasoning the compiler does about flow control.
Iโm not totally against this, but I donโt feel like implementing this myself.
what if it's not used because it's not in many languages sorry
JavaScript, Java and Go, implement exactly this feature, for instance. C++, C and C# have labels with goto
which would cover this use case. I think that covers a lot of code being written today...
Thanks to the latest changes, โญ is no longer in use in the s package. We could use it for the 'continue' statement now.