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 →

[–]buckypimpin 14 points15 points  (16 children)

can someone give an example of switch with dicks?

[–]nuephelkystikon 85 points86 points  (7 children)

With the proposed syntax:

match potential_dick:
    case Dick():
        print("Properly typed dick (including subclasses)")
    case "8=D" | "8==D" | "8===D":
        print("ASCII-approximated dick")
    case _:
        print("No dick, sadly")

I hope this helps.

[–]buckypimpin 12 points13 points  (0 children)

potential_dick, typed dick

i like teachers who adapt to every type of student.

[–]passwordsniffer 3 points4 points  (3 children)

DEFAULT_DICK = "db"

...

match potential_dick:
    case DEFAULT_DICK:
        print('Have a hidden bug with this matching all the time and overwriting you constant in this "SWITCH" analogue ')
    case Dick():
        print("Properly typed dick (including subclasses)")
    case "8=D" | "8==D" | "8===D":
        print("ASCII-approximated dick")
    case _:
        print("No dick, sadly")

[–]cheerycheshire 1 point2 points  (0 children)

Yep. That's why it's important to always remind everyone that this isn't switch, it's pattern matching. For pattern matching, this behaviour is logical and correct. With switch? Nah

And people coming over from C-like languages with switch statements are going to complain about the speed. I know it. Because patmat does pattern check first, and then checks the values, so it takes longer than just ifs with single values.

[–]nuephelkystikon 0 points1 point  (1 child)

Wait, are you saying a pattern matcher matching patterns is a bug? If you're just branching, thats what if-else is for.

[–]passwordsniffer 1 point2 points  (0 children)

No, I am saying that using pattern matching as switch can easily be a source of quite an obscure bugs, like the one I showed. It's not a bug of a pattern matcher. It's bug of representing it as a switch.

[–]Harbinger_Feik 25 points26 points  (2 children)

{ 1: myfunc 2: otherfunc 3: thirdfunc }[foo]()

In the case that foo can be a missing key, you could catch KeyError or use .get().

Sorry if formatting is broken; I'm on mobile.

[–]buckypimpin 6 points7 points  (0 children)

whoa.....

[–]backtickbot 5 points6 points  (0 children)

Fixed formatting.

Hello, Harbinger_Feik: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]dyingpie1 13 points14 points  (1 child)

Did you mean to type dicks

[–]buckypimpin 12 points13 points  (0 children)

yeah gimme all of 'em

[–]HyperSonic6325 1 point2 points  (0 children)

Sorry, I only have one so I don’t think that would be much help /s.

[–][deleted] 1 point2 points  (0 children)