Comments (5)
I think this feature is somewhat problematic and probably superfluous, and would therefore vote against it.
Problematic because of value extraction/variable binding. As your question already indicates: what happens if the user-defined pattern should bind a value to a variable? How is that properly and correctly propagated to the surrounding context? It is a bit like defining a new local variable using exec
/eval
. Moreover: who would have knowledge of the name and nature of the extracted variable? In other words: what kind of code would be aware that the pattern has matching something and bound it to a variable x
, say?
On the other hand, if we say that a user-defined pattern cannot bind to a variable, but would match against a variable, it can suddenly access variable values of the surrounding context, which is where I see a potential security risk. Actually, allowing a user-defined pattern to write to a local variable is probably likewise a very bad idea from a security point of view.
If the example is just as simple as comparing numeric values, you can use guards and simply do case v if v == P1:
without the need for user-defined patterns in the first place.
Finally, yes, the mechanisms discussed in issue #8 with a __match__
method would allow to easily write something like a customised, user-defined pattern if really needed. So, there is actually no need to support something like this on a language level, as it could easily be done by a library (and in a more controlled way).
In a way, I would argue that pattern matching is actually not purely runtime behaviour, exactly because of variable binding. I see it more akin to how local variables behave: while an assignment to a local variable is runtime behaviour, indeed, the compiler determines statically whether a variable is local or not.
from patma.
Agreed. I was probably excited having implemented a bunch of objects that represent the basic abstractions for pattern matching (see file "patma.py" here). But I've since come up with an alternative way to translate patterns into code (see various translate()
methods) and there it would be inconvenient to have to support this.
from patma.
Something like this has come up in https://mail.python.org/archives/list/[email protected]/message/DDYISJOZPJOD2C2RY4HH2XS3YMHBVYWX/
from patma.
Yeah, I saw the post. As far as I understood it, Federico Salerno's point of this is to solve the lead/store issue. To that end, he is basically proposing a somewhat cumbersome implementation of proxy objects with quasi-scopes, rather than binding local variables as in the current PEP. To be honest, I feel that if we really boil down his proposal in that post, most of it just evaporates and leads to basically the same discussions as we already had.
from patma.
Yeah, his proposal is the same as my poorly conceived idea here. But I feel it still needs some kind of rational response, since it's bringing up a new idea (for the list) rather than rehashing something old (like different sigils or keywords to mark loads or stores).
Though maybe we needn't respond until at least one other person supports his idea (this has roughly been my approach).
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.