← Back to context

Comment by Jtsummers

3 days ago

> (What's the deal with prohibiting multiple _ but allowing as many __ as you want?)

What do you mean "prohibiting multiple _"? As in this pattern:

  match [1,2]:
    case [_, _]: print("A list of two items")

That works fine.

> An irrefutable case block is a match-all case block. A match statement may have at most one irrefutable case block, and it must be last.

There is no reason to have this restriction except that some people as a matter of opinion think unreachable code is bad taste and the language grammar should make bad taste impossible to express. It's often useful to introduce such things as a temporary state during editing. For example,

    def foo(x):
        match x:
            case _:
                log.warning("XXX disabled for debugging")
                return PLACEHOLDER
            case int():
                return bar()
            case str():
                return qux()
            case _:
                return "default"

Why should my temporary match-all be a SyntaxError???? Maybe it's a bug. Maybe my tools should warn me about it. But the language itself shouldn't enforce restrictions rooted in good taste instead of technical necessity.

I can, however, write this:

    def foo(x):
        match x:
            case _ if True:
                log.warning("XXX disabled for debugging")
                return PLACEHOLDER
            case int():
                return bar()
            case str():
                return qux()
            case _:
                return "default"

Adding a dummy guard is a ridiculous workaround for a problem that shouldn't exist in the first place.

  • I don't disagree, it should be a warning but not an error. Thanks for clarifying, your original remark was ambiguous there.