Open sungshik opened 1 year ago
Yes; the pattern notation does not have the .
or any other expression notation other than variable names and constructor notations. So no projections, closures, etc.
The proposal is to add a new syntax for equality checks in patterns that is not the implicit variable name anymore, so:
[*_, <int x, x>, *_] := pairs
for [*_, <int x, !x>, *_]
(or some notation to make explicit that equality is checked and not a new variable introduced.[*_, <int x, !(x.pos)>, *_]
, where x.pos
is any expression in Rascal that would be checked for equality against the subject at the position during matching.@PaulKlint would be cool to work on this sometime soon; if not as a welcome diversion.
Ah, ok, I see, thanks for clarifying!
Aside: My actual use case was to use patterns in the LHS of in
. This doesn't work:
rascal><3, _> in toSet(pairs);
|prompt:///|(4,1,<1,4>,<1,5>): Undeclared variable: _
This does work:
rascal>{<3, _>, *_} := toSet(pairs);
bool: true
But I suppose that's a different enhancement ;)
Yes that's another different enhancement, but also cool :) Thanks Sung. Keep em coming!
Aside: My actual use case was to use patterns in the LHS of
in
. This doesn't work:rascal><3, _> in toSet(pairs); |prompt:///|(4,1,<1,4>,<1,5>): Undeclared variable: _
btw, since any generator just generates a stream of booleans, you can write:
rascal><3, _> <- toSet(pairs)
bool: true
I sometimes write like this:
if (<3, _> <- pairs) {
// do x
}
Ah, <-
even looks a bit like ∈
with some imagination ;) Thanks!
True, just a warning, if you use this in a comprehension (or reducer/for) it will repeat for all matching entries. So be careful how you use this pattern ;)
I think, i would write it as: 3 in pairs<0>
if I was in a
comprehension/loop.
On Tue, 6 Dec 2022, 16:08 sungshik, @.***> wrote:
Ah, <- even looks a bit like ∈ with some imagination ;) Thanks!
— Reply to this email directly, view it on GitHub https://github.com/usethesource/rascal/issues/1731#issuecomment-1339524994, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABL3E3RWV5TLKKGS7ZNRKTWL5JGHANCNFSM6AAAAAASVOGM6M . You are receiving this because you commented.Message ID: @.***>
Describe the bug
Parser rejects dot notation in nested patterns in comprehensions.
To Reproduce
Setup:
Ok:
Not ok: