I'm intrigued by PEP 622. It sounds like a great addition, as the current PEP
mentions, the lack of being able to match against a local variable seems like a
great limitation.
I'll admit to being over my head here; last time I did language design was
undergrad CS 15 years ago. However, I was wondering if the following was
considered (if it was, it isn't mentioned in rejected ideas): Force all
assignment to happen through the walrus operator. We have assignment
expressions, let's use them! This then frees up variable names to act and look
like normal matches. The code first mentioned in the overview would then look
like this:
def make_point_3d(pt):
match pt:
case (x := _, y := _):
return Point3d(x, y, 0)
case (x := _, y := _, z := _):
return Point3d(x, y, z)
case Point2d(x := _, y := _):
return Point3d(x, y, 0)
case Point3d(_, _, _):
return pt
case _:
raise TypeError("not a point we support")
Sequence pattern could look like this:
>From sequence patterns:
match collection:
case 1, [x, others := *_]: # or others := *rest
print("Got 1 and a nested sequence")
case (1, x := _):
print(f"Got 1 and {x}")
I'll admit to it not being as elegant, but it's also not as confusing. This
would eliminate the discrepancy between "case name" and "case obj.name".
x = 1
obj.x = 2
match something:
case obj.x:
print('something is 2')
case x:
print('something should be 1....oops')
The problem with just leaving it to a SyntaxWarning is that 1) those get
ignored, leading to bugs. 2) Even if followed, you'll end up with a lot of code
looking something like this:
# x, y previously declared
dummy = SimpleNamespace()
dummy.x = x
dummy.y = y
match something:
case dummy.x:
print('something is x')
case dummy.y:
print('something is y')
...
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/ITHCK2QC7JXEL4XLBR4FEYPYG6KYCTIG/
Code of Conduct: http://python.org/psf/codeofconduct/