Skip to content

Commit 6084bf7

Browse files
committed
docs for macrocall patterns
1 parent 2b436d1 commit 6084bf7

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

docs/syntax/pattern.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ julia> first(it)
139139
3
140140
julia> it[2]
141141
4
142+
143+
julia> @match Int[1, 2] begin
144+
Any[1, 2] => :a
145+
Int[_, _] => :b
146+
end
147+
:b
142148
```
143149

144150
- Dict pattern(like `Elixir`'s dictionary matching or ML record matching)
@@ -251,6 +257,46 @@ c = ...
251257
end
252258
```
253259

260+
261+
Macro Call Patterns
262+
------------------------
263+
264+
By default, macro calls occur in patterns will be no different than its expanded expression, hence the following bidirectional relationship **sometimes** holds:
265+
266+
```julia-console
267+
julia> macro mymacro(a)
268+
esc(:([$a]))
269+
end
270+
@mymacro (macro with 1 method)
271+
272+
julia> a = 2
273+
2
274+
275+
julia> a == @match @mymacro(a) begin
276+
@mymacro(a) => a
277+
end
278+
true
279+
280+
# expanded form:
281+
# julia> a == @match [a] begin
282+
# [a] => a
283+
# end
284+
```
285+
286+
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).
287+
288+
Some examples about string macro patterns:
289+
290+
```julia
291+
@match raw"$$$" begin
292+
raw"$$$" => ...
293+
end
294+
295+
@match "123" begin
296+
r"\G\d+$" => ...
297+
end
298+
```
299+
254300
Custom Patterns
255301
--------------
256302

src/MatchImpl.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ function guess_type_from_expr(m::Module, ex::Any, tps::Set{Symbol})
8080
end
8181
end
8282

83-
ex2tf(m::Module, @nospecialize(a)) =
84-
isprimitivetype(typeof(a)) ? literal(a) : error("invalid literal $a")
83+
ex2tf(m::Module, @nospecialize(a)) = literal(a)
8584
ex2tf(m::Module, l::LineNumberNode) = wildcard
8685
ex2tf(m::Module, q::QuoteNode) = literal(q.value)
8786
ex2tf(m::Module, s::String) = literal(s)

src/Pervasives.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ function _allow_assignment!(expr::Expr)
7575
end
7676
end
7777

78+
function MLStyle.pattern_unref(::Type{E}, self::Function, args::AbstractArray) where E
79+
self(:([$(args...)] :: $AbstractVector{$E}))
80+
end
81+
7882
function MLStyle.pattern_unref(::Type{Do}, self::Function, args::AbstractArray)
7983
foreach(_allow_assignment!, args)
8084

test/pervasive.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,11 @@ end
275275
let x = 1 end && Do[x = 2] => (x==2)
276276
_ => false
277277
end
278+
end
279+
280+
@testcase "typed vector" begin
281+
@test @match Int[1, 2, 3] begin
282+
Any[1, 2, 3] => false
283+
Int[_, _, _] => true
284+
end
278285
end

0 commit comments

Comments
 (0)