News for hackers. Stuff that matters.
Re:Perl 6's given: switch on steroids by combatentropy on Saturday June 01, 2019 @11:58PM (#20073374)
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.

Re:Perl 6's given: switch on steroids by codr7 on Sunday June 02, 2019 @06:50AM (#20074666)
Falling 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.

Re:Perl 6's given: switch on steroids by rat9988 on Sunday June 02, 2019 @02:06PM (#20077005)
Honestly I'd rather use goto than have fallthrough by default in C. Or even better, a Fallthrough keyword as in GO.
Re:Perl 6's given: switch on steroids by b2gills on Tuesday June 04, 2019 @12:17AM (#20091847)
Perl6 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
    }
Re:Perl 6's given: switch on steroids by codr7 on Sunday June 02, 2019 @06:30PM (#20078761)
Wow, 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.

Re:Perl 6's given: switch on steroids by pantalaimon on Sunday June 02, 2019 @09:14AM (#20075230)
Nowadays 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.

Re:Perl 6's given: switch on steroids by WhiteSage on Sunday June 02, 2019 @01:43PM (#20076867)
C++17 includes the [[fallthrough]] attribute built into the language to disable the warning [1].

[1] https://en.cppreference.com/w/cpp/language/attributes/fallth...

Re:Perl 6's given: switch on steroids by melling on Sunday June 02, 2019 @09:32AM (#20075320)
Shouldn’t we optimize for the common case?

The language should do want we want most of the time.

Re:Perl 6's given: switch on steroids by toast0 on Sunday June 02, 2019 @11:30AM (#20076066)
We 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.

Re:Perl 6's given: switch on steroids by melling on Sunday June 02, 2019 @11:54AM (#20076195)
So, 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.
Re:Perl 6's given: switch on steroids by jnordwick on Sunday June 02, 2019 @12:47PM (#20076516)
You'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).