You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
provides ways to circumvent unexpected type bindings
This PR fixes issues involved with the syntaxes to specify expression types.
Currently, `@capture` and `@match` can't distinguish between their syntax
to specify a `:head` of `Expr` and a variable name with underscores.
For example, in the example below, `@capture` recognizes `global_string`
as the syntax to specify `Expr`'s head (i.e. `:string`), not as a simple
variable name:
```julia
julia> ex = :(global_string = 10);
julia> @capture(ex, global_string = n_) # tries to match `Expr(:string, ...) = n_` and bound the matched lhs into a variable `global` and the matched rhs into a variable `n`.
false
```
Since an expression can really have an arbitrary head, `@capture` macro
can't really distinguish them (while we can do some assertion when the
syntax to specify atomic expression type, though).
This PR implements new macros `@capture_notb` and `@match_notb`, which
ignore all the expression type matching syntaxes and provide the ways to
circumvent the issue described above:
```julia
julia> ex = :(global_string = 10)
julia> @capture_notb(ex, global_string = n_) # tries to match `global_string = n_` pattern and bound the matched rhs into a variable `n`.
true
```
These changes aren't breaking but they're somewhat a big change, I'd
like to minor version bump.
Copy file name to clipboardExpand all lines: docs/src/pattern-matching.md
+21-1
Original file line number
Diff line number
Diff line change
@@ -68,7 +68,7 @@ Highlander there can only be one (per expression).
68
68
69
69
### Matching on expression type
70
70
71
-
`@capture` can match expressions by their type, which is either the `head` of `Expr`
71
+
`@capture` can match expressions by their type, which is either the `:head` of `Expr`
72
72
objects or the `typeof` atomic stuff like `Symbol`s and `Int`s. For example:
73
73
74
74
```julia
@@ -91,6 +91,26 @@ Another common use case is to catch symbol literals, e.g.
91
91
92
92
which will match e.g. `struct Foo ...` but not `struct Foo{V} ...`
93
93
94
+
!!! tip "Matching without expression type"
95
+
[Matching on expression type](@ref) can be useful, but the problem is that `@capture` can't distinguish between
96
+
its syntax to specify a `:head` of `Expr` and a variable name with underscores:
97
+
98
+
For example, in the example below, `@capture` recognizes `global_string` as the syntax to specify `Expr`'s head (i.e. `:string`),
99
+
not as a simple variable name:
100
+
```julia
101
+
julia> ex = :(global_string = 10);
102
+
julia> @capture(ex, global_string = n_) # tries to match `Expr(:string, ...) = n_` and bound the matched lhs into a variable `global` and the matched rhs into a variable `n`.
103
+
false
104
+
```
105
+
106
+
To circumvent this issue, MacroTools exports `@capture_notb`, which skips all the expression type matching syntaxes:
107
+
```julia
108
+
julia> ex = :(global_string = 10)
109
+
julia> @capture_notb(ex, global_string = n_) # tries to match `global_string = n_` pattern and bound the matched rhs into a variable `n`.
110
+
true
111
+
```
112
+
113
+
94
114
### Unions
95
115
96
116
`@capture` can also try to match the expression against one pattern or another,
0 commit comments