Skip to content

Commit

Permalink
docs for macrocall patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
thautwarm committed Mar 13, 2021
1 parent 2b436d1 commit 6084bf7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
46 changes: 46 additions & 0 deletions docs/syntax/pattern.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ julia> first(it)
3
julia> it[2]
4
julia> @match Int[1, 2] begin
Any[1, 2] => :a
Int[_, _] => :b
end
:b
```

- Dict pattern(like `Elixir`'s dictionary matching or ML record matching)
Expand Down Expand Up @@ -251,6 +257,46 @@ c = ...
end
```


Macro Call Patterns
------------------------

By default, macro calls occur in patterns will be no different than its expanded expression, hence the following bidirectional relationship **sometimes** holds:

```julia-console
julia> macro mymacro(a)
esc(:([$a]))
end
@mymacro (macro with 1 method)
julia> a = 2
2
julia> a == @match @mymacro(a) begin
@mymacro(a) => a
end
true
# expanded form:
# julia> a == @match [a] begin
# [a] => a
# end
```

However, you can also change the pattern compilation behavior by overloading `MLStyle.pattern_unmacrocall`, whose usage can be found at the implementation of the pattern support for [`@r_str`](https://github.com/thautwarm/MLStyle.jl/blob/master/src/Pervasives.jl#L191).

Some examples about string macro patterns:

```julia
@match raw"$$$" begin
raw"$$$" => ...
end

@match "123" begin
r"\G\d+$" => ...
end
```

Custom Patterns
--------------

Expand Down
3 changes: 1 addition & 2 deletions src/MatchImpl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ function guess_type_from_expr(m::Module, ex::Any, tps::Set{Symbol})
end
end

ex2tf(m::Module, @nospecialize(a)) =
isprimitivetype(typeof(a)) ? literal(a) : error("invalid literal $a")
ex2tf(m::Module, @nospecialize(a)) = literal(a)
ex2tf(m::Module, l::LineNumberNode) = wildcard
ex2tf(m::Module, q::QuoteNode) = literal(q.value)
ex2tf(m::Module, s::String) = literal(s)
Expand Down
4 changes: 4 additions & 0 deletions src/Pervasives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ function _allow_assignment!(expr::Expr)
end
end

function MLStyle.pattern_unref(::Type{E}, self::Function, args::AbstractArray) where E
self(:([$(args...)] :: $AbstractVector{$E}))
end

function MLStyle.pattern_unref(::Type{Do}, self::Function, args::AbstractArray)
foreach(_allow_assignment!, args)

Expand Down
7 changes: 7 additions & 0 deletions test/pervasive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,11 @@ end
let x = 1 end && Do[x = 2] => (x==2)
_ => false
end
end

@testcase "typed vector" begin
@test @match Int[1, 2, 3] begin
Any[1, 2, 3] => false
Int[_, _, _] => true
end
end

2 comments on commit 6084bf7

@thautwarm
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/31847

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.9 -m "<description of version>" 6084bf743831bf0d7c8260586272a186fc413e1b
git push origin v0.4.9

Please sign in to comment.