Copying the idea to higher level languages makes little sense to me. Blindly copying any feature into a language design is a bad idea from my experience.
given 1 {
when Int { say 'integer'; proceed }
if 0 ≤ $_ ≤ 9 { say 'digit'; succeed }
when 1 { say 'one'; proceed }
}
The above will print two lines `integer` and `digit`. It will not get a chance to run the second `when` block because of the `succeed` that gets run before it.If you want the `given` block to return a value, `succeed` can be given a value.
my $result = do given $example {
when 1 { 'one' }
when 2 { succeed 'two' } # succeed is redundant here
if $_ == 3 { succeed 'three' } # acts like a `when` block and returns "three"
…
}
---Perl6 is “supposed” to have a `goto` keyword, but nobody has implemented it. Mainly because of a lack of need.
One of the few good uses of `goto` in a higher level language is to have a portion of a function always run at the function exit. Perl6 has `LEAVE`, `KEEP`, and `UNDO` which handles this nicely.
sub foo (){
my $fh = open 'example.txt', :r;
LEAVE $fh.close; # close $fh at scope exit
… # use the opened file handle
}
Technically it would probably be better to have the `.close` be conditional on whether the $fh was successfully opened. sub foo (){
my $fh = open 'example.txt', :r;
LEAVE with $fh {.close}
without $fh {.throw}; # throw the error in `$fh`
# (This isn't necessary, as it will get thrown as soon as you use it.)
… # use the opened file handle
}That's a seriously ugly solution, but I guess that could be considered a good thing.
If you want to have a fall-through, you have to annotate it with, e.g /* fall-through */
This is of course not ideal, suddenly comments have to be considered by the compiler. But I guess it's the only way to fix this in a backwards compatible way.
[1] https://en.cppreference.com/w/cpp/language/attributes/fallth...
The language should do want we want most of the time.
With C style switch, you can fall through or break. In languages that don't provide fall through (say Erlang's case expression), if you have something that fits fall through, you need to either duplicate the inner code, or have a more verbose construction with multiple phases of matching.
You are thinking about where a value is used to determine which block of code to run, like a ramped up conditional or an if-else-if chain.
C's switch is a more a complex goto statement. All the case statements are labels and the switch does a goto label for the correct value. Those labels just need to be inside the switch block. This is why you can switch into the the middle of a looping construct or if-statement. In this way, break is kind of the oddity and sugar for goto past end of the switch block. This allows for some complex control flow that is kind of useful in a language without much else to offer in that regard (besides the ugly setjump hackiness).
In other languages, I would settle for a switch-statement like Go's, which by default breaks instead of falls through. The common switch-statement seems like a prank: If you omit the break, the compiler throws no error, like when you forget a closing brace. Instead the program silently does the opposite of what you meant.
I am less afraid of forgetting one, more of one day accidentally deleting one. Every once in a while in Vim I order my keystrokes wrong and a random line disappears. Usually it catches my eye, but it is unnerving. The risk is eternal.
You would want to leave the switch-statement as is, for backward compatibility, and come up with a new key word to set off the improved version, like Perl did with "given."
With all of the ongoing improvements to JavaScript, I'm surprised this has not been addressed. And in general with most languages I am surprised that their flow control is so stunted.