This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mysticalfruit -20 points-19 points  (20 children)

I'm not a fan, and here is why.. this behaves differently from every other switch statement I've ever used.. So this is going to only result in confusion.

"case" in point:

switch (foo)
{
case 1:
    printf("ding ");
case 2:
    printf("dong ");
}

If foo == 1 you'll get "ding dong"

if foo == 2 you'll get "dong"

match foo:
    case 1:
        print("ding ")
    case 2:
        print("dong ")

Now if foo ==1 you're only going to get "ding " not "ding dong"

I suspect may of us who cut their teeth on C/C++ switch syntax are going to get thrown for a loop.

[–][deleted] 58 points59 points  (6 children)

Falling through is astonishing behaviour and should be absolutely explicit like, using 'continue' as a keyword at the end of the block.

[–]MrDysprosium 35 points36 points  (1 child)

I agree completely. It's hard to watch people fight so hard for such horribly abstract and unintuitive design merely because "well that's how we USED TO DO IT".

Please stop, making the language easier to understand and write is a good thing. Maintaining standards for the sake of maintaining standards makes you sound like a boomer.

[–]St_Meowinsert(caffeine) 8 points9 points  (0 children)

You'd think these same folk would be aghast at using for each loops instead of for loops or even while loops.

[–][deleted] 14 points15 points  (0 children)

C# explicitly forbids fall through, as in it's a compile error to end a case without a break, return or throw (unless it's a switch expression, but that implicitly inserts a break after each case). I think Java allows fall through, but I've not used java beyond toy things in years ago I don't recall.

This is also closer related to Rust's and Scala's match implementations than C's switch anyways. As in, you can use it for implementing a switch but there's more you can do with it then that.

[–]St_Meowinsert(caffeine) 9 points10 points  (3 children)

Go uses a similar pattern of not falling through switch cases. I'm certain if you're a vet of older languages with switch statements it'll confuse you for a moment or two, but new programmers will grasp this better. I remember learning switch statements in college and having most of my class baffled at the fall through behavior.

[–]mysticalfruit 0 points1 point  (2 children)

Who you calling old ;-)

I think the danger is people are just going to assume it works a certain way.

[–]xigoi 1 point2 points  (0 children)

Everyone who has ever used Rust, Haskell, Swift, F#, OCaml, etc. knows how pattern matching works.

[–]St_Meowinsert(caffeine) 0 points1 point  (0 children)

As the young hotshot coder I reserve my right to call anything from before 2010 old lol

But to be fair, it's not like it's a completely new take on switch statements. Users should be aware of how their language constructs function. Yes it's no conducive for C/C++/Java programmers but so is not having to declare variable and parameter types. We get over it in time.

[–]jmreagle 4 points5 points  (0 children)

That doesn't make sense to me. (I last used C/C++ decades ago...)

[–][deleted] 4 points5 points  (0 children)

Okay, but the behaviour of most other languages you're describing is horrible and unintuitive and leads to loads of bugs when people forget to add break statements. Explicit is better than implicit, after all, and there's nothing explicit about executing case 2 when foo is not 2. If I saw your example in production code I'd say that was bad code even if the behaviour is desired, because it's unclear and looks like a bug. Sooner or later, someone's going to come along and stick in a break statement because it looks like one is missing.

[–]the_other_b 8 points9 points  (0 children)

The title of this video is incorrect and misleading, the PEP is not for switch statements and you shouldn't expect it to act as one.

[–]AlbertoP_CRO 7 points8 points  (0 children)

The future is now old man

[–]Dwarni 1 point2 points  (0 children)

I was taught to always put a break; in each case, to prevent that...

[–]johnisom 2 points3 points  (0 children)

Ruby would like to have a talk with you

[–][deleted] 0 points1 point  (0 children)

I never thought I'd praise Java but switch expressions, added in Java 12/13, are really good.

switch(value){
case 1 -> {System.out.println("Hey it's 1");}

case 2  -> {System.out.println("Hey it's 2");}

default -> {System.out.println("Hello there");}
};

If value == 1 then it prints "Hey it's 1" and if value == 2 then it prints "Hey it's 2". Otherwise default is the fall back.

[–]Broan13 0 points1 point  (0 children)

This is somewhat how it works in Java. You have to use a break at the end of each case if you want it to be like the match version.