Alternative proposal by Ivan, in PEP form #19
Comments
Important note: ideas marked as rejected there are not really rejected, I just wanted to summarize all alternatives, on many of them I was 50-50. |
Btw, it is interesting how close are the proposals, it is a good sign! |
After quickly scrolling through issues, it seems to me all ideas where we diverge are in the 50-50 zone I mentioned. Couple exceptions:
|
Let me summarize what I see as the main differences.
UPDATE: Here are some more differences:
About the
|
It is actually not that different. The general logic is like this: interpreter asks an object: "Can you represent/unpack yourself as something like this?" and the object answers: "yes/no" and in "yes" how exactly. The details of how the answer is represented, as a custom object, or tuple + dictionary is less important. What is more important here is how much context you give to the object. There are two extremes here:
I think both extremes are sub-optimal. For the second there are some arguments in the draft, for the first extreme I think the issue is that it is not flexible enough. I think the best option is somewhere in the middle, so that you ask an object "can you represent/unpack yourself like this: two things and z=third thing?" In my proposal I pass the literal patterns as part of the context, but maybe it is still to much context. I think we should at least pass this data to
One of the motivation for this is that I believe |
Forgot about one thing: moving About other things:
|
One more deviation you forgot, what to do in case if all patterns fail? I propose to raise an exception. The main motivation is that most cases I have seen are rather fall where |
About We considered various APIs for Regarding your specific example, I don't see the ambiguity at all. Clearly The matching of Do we at least agree that Apart from the complex signature, there's also the fact that in your proposal, every class must implement quite a bit of |
Yes, but what if all three arguments are required? I mean if one literally defines: @dataclass
class Point3D:
x: int
y: int
z: int To me In general I agree that we should put as much burden on the interpreter as we can here. But give it the chance to detect all possible ambiguous/impossible matches. |
|
Compromise: we could specify that a pattern is only allowed to bind a name once (or only once in each branch of |
OK, let's drop it. It is not really needed if we have
Potentially we can also allow
I totally agree about the keyword args, but not so much about positional. I think we should some opportunity to the implementer to add some strictness here, like |
|
Probably you meant "there are no precedents"? |
👍
Oops, sorry, yes that was a bad typo :-) |
Okay, fixed the typo in the original. |
I would not expect |
Also, as far as name matching goes: EIBTI. I still favor something like (It's interesting that in LISP, there's this concept of 'quoting' a name, so that you can refer to the name without dereferencing what it holds - much like the C++ reference operator, BTW one of the things I appreciated reading Ivan's PEP was the analysis of existing code patterns, specifically the statistics around usage of isinstance(). Any far-reaching change to a mature and popular language needs to be well-grounded in fact, and not just airy theorizing :) I think the main points of divergence surround the semantics of |
Hm, then we definitely need to set
I'm not a fan of using ellipses as part of pattern syntax. It's too ambiguous in docs etc. So let's use the second form.
Then let me propose that the default is that a plain name is a variable binding for value extraction, while all dotted names are named constants, and if you really need to use a plain name as a named constants (e.g. example 5 in EXAMPLES.md uses Why mark named constants instead of bound variables? Because in 95% of the cases you'll want the latter. Match statements will be littered with things like Also, let me propose that the marking would be a leading As to data, example 5 is the only of the six motivating examples chosen by Brandt from real code that uses a dot-free name. (But I'm still in favor of the lowercase/uppercase rule myself.) |
I really like this idea. I was lately thinking that requiring pattern to be dotted to be considered a reference rather than target will give some push towards using enums instead of plain integer constants, which is probably a good thing. But supporting only enums would be definitely too much, so using leading
|
I also agree that the single dot might be a good compromise here. It also feels not too far off as similar syntax is used for imports. In principle, however, we could also consider to have something like |
First, please let me also welcome Ivan to the team. It was very interesting to read Ivan's draft PEP. I like the different perspective and found the analysis of use cases brilliant. However, there are a few elements with which I find myself rather disagreeing (apart from many points in which we probably all agree). Overall, I have the impression that Ivan's proposal is primarily coming from types, ADTs, and the perspective of performance (please correct me if I am wrong, though). Many of the proposal's elements are about strictness in one way or another. For instance, performing an Let me pick up a few specific and minor points (I will add more comments to the corresponding issues).
|
@Tobias-Kohn some of your comments are in a sense outdated, because Guido already convinced me. The updated version (that I propose to take as a starting point) is in PR #25 |
Yes, I know that some of it is already outdated or really just a minor issue. But I felt your PEP deserves a proper reaction, even if it ends up being basically just a "I agree with Guido as well" :-). |
Ivan Levkivskyi has independently developed a PEP for matching. This has not been publicly shared yet, but I figured it would be okay to share in this private repo.
https://github.com/ilevkivskyi/peps/blob/pattern-matching/pep-9999.rst
The text was updated successfully, but these errors were encountered: