6 ms·
This is nice. In other languages, I would settle for a switch-statement like Go's, which by default breaks instead of falls through. The common switch-statemen
by combatentropy 7y ago
This is nice.
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.
- codr7 7y agoFalling through sort of makes sense in C, which is underdefined by design to allow pulling whatever dirty tricks needed to get where you're going. 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.
- rat9988 7y agoHonestly I'd rather use goto than have fallthrough by default in C. Or even better, a Fallthrough keyword as in GO.
- codr7 7y agoWow, had no idea about fallthrough in Go and I've written plenty. Thanks. That's a seriously ugly solution, but I guess that could be considered a good thing.
- b2gills 7y agoPerl6 has a fallthrough keyword named `proceed`. It also has a keyword named `succeed` which is the default for `when` blocks. 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 }
- pantalaimon 7y agoNowadays GCC warns about implicit fall-through. 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.
- melling 7y agoShouldn’t we optimize for the common case? The language should do want we want most of the time.
- toast0 7y agoWe should not optimize for the common case at the expense of making the uncommon case impossible. 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.
- melling 7y agoSo, 99% of the time to need to be more verbose to handle the 1% case? I’d rather wrap the duplicate code in a function and call it in each instance.
- jnordwick 7y agoYou're thinking about C's switch statement the wrong way. 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).