patma
patma copied to clipboard
Consider allowing negative match pattern
Probably not very important, but while reading code it looks like a negative match may be useful. For example, when people write something like not isisnstance(x, Foo) or x.y != "bar". A general rule would be that match succeeds is the pattern fails. Example syntax may use !:
match expr:
as BinaryOp(left=!IntExpr(value=0)):
...
We may consider this case in addition to | and &.
Hm, we already decided we weren't going to need & (#14), and I think this falls in the same category of "theoretically useful". I doubt such examples will be very common, and it could be expressed alternatively like this:
match expr:
as BinaryOp(left=IntExpr(value=0)):
pass
as BinaryOp(...):
...
Yeah, just opened an issue for completeness. We can probably close this. How do you track ideas on which there is an agreement? Also, are we going to include all these in the final PEP? At some point we should probably start working on an actual PEP.
Sorry for too many meta questions: is anyone already working on some PoC implementation?
How do you track ideas on which there is an agreement?
Currently just in my head. :-) We could add some emoji to the title.
Also, are we going to include all these in the final PEP?
I think it's fair to bring up any issue that had at least one supporter in a "Rejected Ideas" section, however briefly. Some ideas may be revived later, such as additional matching operators (like !) or the shorthand notation. Others are mutually exclusive with key decisions, e.g. whether to use case or as.
At some point we should probably start working on an actual PEP.
I wonder if we could start with yours and just make edits to reflect our eventual positions. You did a lot of work on it already and it seems valuable.
Is anyone already working on some PoC implementation?
No, although I have some Python code in this repo (also see README.md and EXAMPLES.md).
I wonder if we could start with yours and just make edits to reflect our eventual positions.
I can prepare some kind of a merged draft by updating the sections were we have agreement, and replacing the sections that are not clear yet with TDB or some partial stubs. I will try to make a PR on weekend.
Awesome!
I agree that we should not add negative patterns. I think that negative patterns are usually better expressed either as proper if statements, or by an "else"-clause in the match statement. Negative patterns can quickly become rather complicated.
A paper on another language that has negative match patterns states: The negative pattern !P, which succeeds if P fails, is only rarely useful. (Bloom, Hirzel: Robust Scripting via Patterns). They then go on to remark thatn !P does not bind any variables, even if P does, and that therefore particularly double negation is useful to make bindings local, i.e. basically allowing !!(x, x) to mean a tuple with two identical elements, but without actually introducing a variable x.
So, I feel there is some indication that negative patterns are not needed, and, which might even be more important to us, that having negative patterns has ramifications on the question of variable bindings, which might make the overall semantics much more complex (without really adding that much).
Okay, into "Rejected Ideas" with this!