JuliaServices / Match.jl

Advanced Pattern Matching for Julia
https://juliaservices.github.io/Match.jl/latest/
Other
242 stars 22 forks source link

Matching Expr #3

Closed magistere closed 11 years ago

magistere commented 11 years ago

Is it possible to match Expr expressions? The following script gives error ERROR: args not defined

using Match

function get_args(ex::Expr)
    @match ex begin
        Expr(:call, {:+, args}, _) => args
        _ => "None"
    end
end

println(get_args(:(x+1)))
println(get_args(Expr(:call, :+, :x, 1)))
kmsquire commented 11 years ago

It's intended to work, since Exprs are just types, but I hadn't tested it. I'll take a look.

kmsquire commented 11 years ago

Thanks for the report. I just pushed a fix. Update and let me know if it works (or not) for you, or if you find any other issues.

Note that for this to work, you'll need to add ellipses to the args variable inside of the call:

    @match ex begin
        Expr(:call, {:+, args...}, _) => args
        _ => "None"
    end

Cheers, Kevin

magistere commented 11 years ago

Kevin, thank you for quick fix and updating the package in repository. It works great now. When I read package documentation on array matching, I've found one discrepancy in chapter "Extract first element, rest of vector". Now type of b is SubArray, not Array.

julia> @match([1:4], [a,b...])
(1,[2,3,4])

julia> a
1

julia> b
3-element SubArray{Int32,1,Array{Int32,1},(Range1{Int32},)}:
 2
 3
 4

I'd like to correct this myself, but I assume documentation sources are not on the GitHub.

kmsquire commented 11 years ago

Glad to hear it! The documentation sources are in the doc directory.

kmsquire commented 11 years ago

And yes, the change from Array to SubArray was made a little while back, and I forgot to change the documentation. Now that you brought it to my attention, I'll get to it eventually, but a pull request would be quite welcome.

magistere commented 11 years ago

Done #5