Skip to content
This repository has been archived by the owner on Nov 21, 2022. It is now read-only.

How to spell anonymous default case? #6

Open
gvanrossum opened this issue Apr 11, 2020 · 2 comments
Open

How to spell anonymous default case? #6

gvanrossum opened this issue Apr 11, 2020 · 2 comments
Labels
accepted Discussion leading to a final decision to include in the PEP fully pepped Issues that have been fully documented in the PEP

Comments

@gvanrossum
Copy link
Owner

We could either use case _: ... or else: ....

In favor of else, this is what you use for if-elif-elif.

Against else:

  • We already have too many different uses of else (for-else/while-else, if-else, try-except-else).
  • You don't need it, since you can write case _: ...
  • The special meaning of _ as a pattern is useful inside nested patterns too, e.g.
match x:
    case [1, 2, _]: print("Starts with 1, 2")
    ...
@Tobias-Kohn
Copy link
Collaborator

I feel this discussion is closely connected to issue #5 (as I mentioned there) and depends on the semantics that we want to convey. If matches have to be exhaustive, I would argue strongly against the use of else, otherwise I would just favour case _ ;-).

My issue with else is that it is IMHO not necessarily clear what it connects to. This is different with if-elif-else chains, as the else there is an intrinsic part of the entire design (as an extension of if-else). Notwithstanding, having taught Python for quite some time now, I often found my students struggling with the semantics of if-if-else vs. if-elif-else, making me quite aware of the difficulty of such constructs. Moreover, a couple of students replaced all ifs with elifs to have a consistent picture of elif-elif-elif. Consistency in the syntax (i.e. using case everywhere) might therefore help learners.

Anyway, using the case _: variant, we would have something like:

match x:
    case [1, 2, _]: print("Starts with 1, 2")
    case _: print("Anything else")

However, if we went for an else, I would place an else one level higher like so:

match x:
    case [1, 2, _]: print("Starts with 1, 2")
else:
    print("No pattern matched the value")

The reason for this being that the else belongs logically to the match-statement, expressing that no pattern actually matched the value. An else on the same level as the cases would read much more as if belonging to that last case in the list. While we probably all picture the cases to replace if-elif-else-chains, this is not necessarily obvious to others. In principle, we could even have something like (well, now, this looks very ugly to me, to be honest):

match x:
    case [1, 2, _]:
        print("Starts with 1, 2")
    else:
        print("Starts with something else...")
    case [_, 9]:
        print("and ends in nine!")

@gvanrossum
Copy link
Owner Author

Okay, there's a principle of using the fewest ingredients necessary somewhere. Let's use 'case _:' instead of 'else:'.

@viridia viridia added the accepted Discussion leading to a final decision to include in the PEP label May 20, 2020
@viridia viridia added the fully pepped Issues that have been fully documented in the PEP label Jun 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted Discussion leading to a final decision to include in the PEP fully pepped Issues that have been fully documented in the PEP
Projects
None yet
Development

No branches or pull requests

3 participants