Comments (9)
I am going to mark this one as 'accepted' since we have come to a conclusion.
from patma.
Yes, in a functional language it has to raise an exception because an unmatched expression does not evaluate to a value. But since Python is statement-oriented, I do not see any problem with leaving it to just do nothing.
Moreover, Scala's compiler tries to make sure that every permissible value in a match expression matches at least one case, and you might get a compilation warning/error otherwise. In practice, however, I found that I often have to put in something like a case _ => // can never happen
or case _ => // just do nothing
. Hence, I would agree and favour a "do nothing" semantics.
This might actually be linked to the syntactic question of issue #6: if matches are meant to be exhaustive, then I find case _ =>
to make much more sense to represent the "exactly one of" semantics.
Just to be complete here: in my article I had presented an argument in favour of raising an exception that was primarily based on multiple dispatch. In languages like ML, function dispatch and pattern matching are so closely related and intertwined that it is difficult to draw a clear border. If pattern matching is used in such a context, we would probably all agree that spam(1, 2)
should either execute a function or raise a TypeError
otherwise, but never just silently do nothing if the arguments do not match the parameters.
In Python and the current discussion, however, we have a completely different case. In version 2, Python supported simple pattern matching/tuple unpacking in function arguments, but has departed since. Type annotation for parameters is very specifically not checked or enforced. I feel that extending pattern matching to functions would be counter Python's current direction and the considerations that hold for functional languages are thus simple not applicable to Python.
from patma.
You have convinced me. Default do nothing is Pythonic.
from patma.
A curious observation in favour of raising by default + one-off matches: out of six examples in EXAMPLES.md
three examples have raise
statement in the else
part, one probably should have it, and two others can be naturally expressed as one-off matches.
This matches my observations, it is always either 1-2 special cases, or a battery of exhaustive cases.
from patma.
I have to admit that's not a bad argument. Examples 1-3 all have a raise
that basically says "no cases matched". Ex. 4 has somewhat warped flow (it uses continue
! but I'd rather not use that, and duplicate the default processing in both the inner and outer default case), and Ex. 5 translates to case _: return None
. Ex. 6. could use the one-off match (and otherwise would need case _: pass
).
from patma.
Actually, after thinking more about this it seems to me we are better with not raising by default (and therefore not needing the one-off matches). The point is that this whole PEP is a pretty big addition to the language, so it would make sense to minimize possible friction. I could imagine that majority of the community may be disappointed by raising by default (even though much later they will likely want it). Also not having one-off matches will make the PEP conceptually a bit smaller and easier to grasp.
(Also for people who want more the safety there is already a section that specifies static checkers should check exhaustiveness.)
from patma.
That's the main reason why I've been pushing back too. Would you mind sending a PR for our PEP? It should probably mention both "raise if no case matched" and the one-off syntax in the Rejected Ideas section.
I do think that this is a one-way-door; I don't think we can switch to raising once the code has been released. (But static checkers should feel free to insist on completeness in cases where it's checkable.)
from patma.
I suppose you could introduce a 'strict' keyword later to enable this behavior.
from patma.
Would you mind sending a PR for our PEP?
I can send a PR tomorrow or on weekend (unlikely today).
from patma.
Related Issues (20)
- Phrasing update in abstract and overview HOT 2
- Disallow guards when using an "open pattern"? HOT 11
- Can the compiler move guards around? HOT 7
- JSON example HOT 5
- Add link to DLS paper to PEP 635 HOT 1
- Reach out to Pablo for code review HOT 1
- Use "as" for named patterns HOT 2
- Raise SyntaxErrors for anything following irrefutable patterns
- Finish AST validation for patterns
- Catch remaining errors in the compiler HOT 1
- Unify the PEP 634 and implementation grammars HOT 2
- Review progress for the reference implementation HOT 5
- Thomas Wouters' objections HOT 13
- Nick Coghlan's (revised) PEP 642 HOT 1
- Dedicated AST nodes for patterns? HOT 5
- PyCon 2021 HOT 10
- work error with zip param "strict=False" on py386 HOT 1
- Please document the design decisions behind PEP 634. HOT 7
- Case goto's? HOT 1
- PyCon JP 2021 HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from patma.