Flexible active pattern syntax
jariji opened this issue · comments
julia> @active *{l}(r) begin
Some(r/l)
end
julia> @match 60 begin
*{10}(x) => x
end
6.0
This syntax is a little cumbersome. Would it be possible to have a pattern like 10 * x => x
?
julia> @active l * r begin
Some(r/l)
end
ERROR: LoadError: malformed active pattern definition: l * r
You prefer the first operand of *
to be a value, and the second is a pattern, which doesn't seem a good practice to me.
However, having such patterns in your special context is not bad at all, you can use custom patterns:
julia> using MLStyle
# As a pattern, `MyContext(f(a, b))` is equal to `f{a}(b)`:
julia> struct MyContext end
julia> function MLStyle.pattern_uncall(::Type{MyContext}, to_pattern, _, _, arguments)
@match arguments begin
[Expr(:call, f, fst, snd)] => to_pattern(:($f{$fst}($snd)))
end
end
julia> @active *{l}(r) begin
Some(r/l)
end
julia> @match 10 begin
MyContext(5.0 * a) => a
end
2.0
The special context idea with pattern_uncall
works for me.
You prefer the first operand of * to be a value, and the second is a pattern, which doesn't seem a good practice to me.
I didn't understand this point. Why would operand order matter for patterns?
I didn't understand this point. Why would operand order matter for patterns?
5.0
in 5.0 * a
is a value, but generally, like in the following code, 5.0
becomes a pattern.
@match value begin
5.0 => ...
end
Without any special annotations, the meaning of 5.0 * a
seems a little vague to me.
Ah yes, I see, it is potentially ambiguous. It is assumed in 5.0 * a
that 5.0
is a value and a
is a pattern but it could be otherwise. 5.0
could be a pattern and a
could be a name in the enclosing scope. Even *
could be a pattern.
Thank you.