Yet Another Switch-Case Syntax Proposal
Hello everybody. I firstly proposed this syntax in the python-list. As I already wrote there, I read PEP 3103 and I'm not completely satisfied by any of the proposed solutions.
My first idea was a little different, this is my final proposal after a short but good brainstorm:
switch_stmt ::= "switch" switch_expr "case" case_expr ":" suite
("case" | "elcase" case_expr ":" suite)*
["else" ":" suite]
switch_expr ::= expression
case_expr ::= expression_list
- if case_expr is a tuple, the case suite will be executed if switch_expr is a member of the tuple
- if case_expr is not a tuple, the case suite will be executed if switch_expr == case_expr
- if a case_expr is checked, any subsequent elcase statements are skipped, and the next case statement is performed, of there's one. This is completely identical to if - elif.
Example:
briefing_days = ("Tue", "Thu")
normal_days = ("Mon", "Wed", "Fri")
switch day case normal_days + briefing_days:
go_to_work = True
day_type = "weekday"
case normal_days:
lunch_time = datetime.time(12)
meeting_time = datetime.time(14)
elcase briefing_days:
lunch_time = datetime.time(11, 30)
meeting_time = datetime.time(12, 30)
else:
go_to_work = False
day_type = "festive"
lunch_time = None
meeting_time =None
A simpler example:
switch tarot case 0:
card = "Fool"
elcase 1:
card = "Alan Moore"
elcase 2:
card = "High Priestess"
participants (11)
-
Andrew Barnert
-
Bruce Leban
-
Guido van Rossum
-
Joao S. O. Bueno
-
João Bernardo
-
Lucas Malor
-
Mark Lawrence
-
Michael Foord
-
Oleg Broytman
-
Skip Montanaro
-
Stephen J. Turnbull