Currently, if “match-case” is used together with “for”, the code need a lot of indents and looks just like stairs.
for event in get_events():
match event:
case Event(MOUSEMOTION, (640,480,100,100)):
print("Mouse is floating on the button.")
case Event(MOUSECLICK, (640,480,100,100)):
print("Click!")
Since “match-case” is often in a “for” loop, why not make a simplified “match-for” like this?
match for event in get_events():
case Event(MOUSEMOTION, (640,480,100,100)):
print("Mouse is floating on the button.")
case Event(MOUSECLICK, (640,480,100,100)):
print("Click!")
Doesn’t really add very much - you’re still doing the same things in the same way, but with fewer indents. Maybe consider a “half-indent” on the match statement, if you want to emphasize that it’s really just part of the same structure as the for loop? But adding this as syntax seems unnecessary, as it binds together things that aren’t particularly related.
In general, programming means composing things out of logically-coherent building blocks, where each primitive has a useful meaning on its own, like “add these values” or “iterate over this collection”. Different languages have different ideas of what constitutes a “building block” (notably, domain-specific languages often have VERY high-level building blocks like “send private message to user” or “move object towards player”), but most languages agree that their building blocks should individually be useful, and generally not be redundant.
for event in get_events(): match event:
case Event(MOUSEMOTION, (640,480,100,100)):
print("Mouse is floating on the button.")
case Event(MOUSECLICK, (640,480,100,100)):
print("Click!")
for event in get_events(): match event:
^^^^^
SyntaxError: invalid syntax
You can avoid an indentation by utilizing another function:
def get_events():
return [1, 2]
def dispatch(event):
match event:
case 1:
print("Mouse is floating on the button.")
case 2:
print("Click!")
for event in get_events():
dispatch(event)