thautwarm / MLStyle.jl

Julia functional programming infrastructures and metaprogramming facilities

Home Page:https://thautwarm.github.io/MLStyle.jl/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bug: splatting with type is not matched

Roger-luo opened this issue · comments

MWE:

using MLStyle: @match
@match :(A[2, 4]) begin
    Expr(:ref, name::Symbol, size::Int...) => begin
        @show size
    end
end

The pattern should not be Expr(:ref, name::Symbol, size::Int...), think of how it gets constructed:

name = :A
size = [1, 2]
Expr(:ref, name::Symbol, size::Int...) 

ERROR: TypeError: in typeassert, expected Int64, got a value of type typeof(size)

As match is reversing construction, we can think about the construction firstly:

# construct
sz = [2, 4]
@assert Expr(:ref, name::Symbol, [e::Int for e in sz]...)  == :(A[2, 4])

# so the deconstruction is:

@match :(A[2, 4]) begin
       Expr(:ref, name::Symbol, [e::Int for e in size]...) => begin
            @show size
        end
end
# size = [2, 4]

P.S: do not use Int[e for e in size], because it is Vector{Any}:

julia> Expr(:ref, name::Symbol, [1, 2]...).args
3-element Vector{Any}:
  :A
 1
 2