Comments (7)
The argument for keeping the syntax of patterns the same as that for expressions, but giving it a (quite!) different semantics, seems weak, since the semantics really are so different already -- e.g. 1|2
in a pattern doesn't mean the same as 3
, it matches either 1
or 2
.
Presumably this means that we shouldn't allow any other operators (e.g. 1+2
should be disallowed), except unary -
(for negative numbers only). Oh, and if we support complex numbers we should allow +
to add a real and an imaginary part, e.g. 1+0j
.
from patma.
More arguments against type annotations:
- Annotations are for static checkers, but matching is runtime behavior.
- What should
case [x: list[int]]
check?isinstance(x, list) and all(isinstance(a, int) for a in x)
? - What about
case [x: list[T]]
?
from patma.
FWIW, impossibility to use list[int]
is the main reason why I don't like the annotation syntax.
from patma.
So what was the final judgment on this? I know it was marked as rejected, but it's not clear from the conversation why.
from patma.
Mostly because of TOOWTDI, and because we don't know what we would do with things like list[int]
-- Python currently disallows isinstance(x, list[int])
, and it would be yet another source of inconsistency.
from patma.
OK after reading this a couple more time I think I misunderstood what was being proposed. You were proposing to use a syntax similar to type annotations to replace or substitute the pattern expression syntax proposed in other threads.
However, that is not what I thought you were proposing - I thought the proposal was to add type annotations in addition to the pattern expression. Something like this:
case [a := list: list[int], b := str]:
or possibly
case [a: list[int] := list, b := str]:
What this is saying is that the pattern expression matches a list, and should be assigned to the variable a
; and, oh by the way you static checkers, the type of that list just happens to be list[int]
. Like all other type annotations, it has no effect on runtime and is only meaningful to static type checkers.
The problem with this, of course, is that it doesn't work for patterns not enclosed in brackets:
case a := list: list[int]:
...because now you have an ambiguous grammar on colon.
from patma.
Yeah, I think the "obvious" way to type extracted variables is to explicitly annotate them before the match block. Colons/parentheses already have multiple meanings in patterns, and giving them any more just looks like unreadable screen-grit-soup to me. 🙂
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.