Skip to content

Commit

Permalink
Update pattern-matching.md with example
Browse files Browse the repository at this point in the history
  • Loading branch information
jkroso committed May 9, 2024
1 parent bebf75d commit 74d55fa
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions docs/src/pattern-matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,18 @@ Another common use case is to catch symbol literals, e.g.

which will match e.g. `struct Foo ...` but not `struct Foo{V} ...`

If you want to match quoted symbols and happen to know that Julia's parser parses
them as `QuoteNode` then you might try `@capture(ex, s_QuoteNode)` and discover that
it never matches. This is because expressions are normalized before being matched
against the pattern. And `QuoteNode` gets normalized to `quote` so to match a quoted
If you want to match on quoted symbols like `:(:a)` you might look at the AST Julia
produces and expect to be able to use the type `QuoteNode` in your pattern. Hence,
you would expect the following to produce `1`:

```julia
@match :(:a) begin
s_QuoteNode => 1
s_quote => 2
end
```
However, it evaluates to `2` because MacroTools normalizes expressions before comparing
them to your pattern. And `QuoteNode` gets normalized to `quote` so to match a quoted
symbol you will need `@capture(ex, s_quote) && s.args[1] isa Symbol`

### Unions
Expand Down

0 comments on commit 74d55fa

Please sign in to comment.