Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parametric types #26

Open
MasonProtter opened this issue Dec 26, 2019 · 3 comments
Open

Support parametric types #26

MasonProtter opened this issue Dec 26, 2019 · 3 comments
Labels
bug Something isn't working question Further information is requested

Comments

@MasonProtter
Copy link

Rematch can't handle pattern matching on parametric types:

julia> @match Val(2) begin
           Val{2} => "hi"
           Val{x} => "bye"
       end
ERROR: LoadError: Unrecognized pattern syntax: Val{x}
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] handle_destruct(::Symbol, ::Expr, ::Set{Symbol}, ::Array{Expr,1}) at /Users/mason/.julia/packages/Rematch/Dgg7p/src/Rematch.jl:187
 [3] handle_match_case(::Symbol, ::Expr, ::Expr, ::Array{Expr,1}) at /Users/mason/.julia/packages/Rematch/Dgg7p/src/Rematch.jl:213
 [4] handle_match_cases(::Expr, ::Expr) at /Users/mason/.julia/packages/Rematch/Dgg7p/src/Rematch.jl:237
 [5] @match(::LineNumberNode, ::Module, ::Any, ::Any) at /Users/mason/.julia/packages/Rematch/Dgg7p/src/Rematch.jl:268
in expression starting at REPL[13]:1
@ghost
Copy link

ghost commented Jan 2, 2020

I think you've structured your pattern incorrectly.

Val(2) doesn't produce Val{2}, it produces Val{2}(). It works (sort of) with the parens:

julia> @match Val(2) begin
           Val{2}() => "hi"
           Val{3}() => "bye"
       end
"hi"

julia> @match Val(3) begin
           Val{2}() => "hi"
           Val{3}() => "bye"
       end
"bye"

But you're right that it still doesn't seem to work with variables in the type parameter:

julia> @match Val(2) begin
           Val{2}() => "hi"
           Val{x}() => "bye"
       end
ERROR: UndefVarError: x not defined

EDIT: Okay never mind, I didn't realize, apparently you can also just put the type there, and it will match on that? In which case, you're right the example you posted is broken.

julia> @match 2 begin
           Int => "hi"
           x => "bye"
       end
"hi"

But i'm not sure if @match intended to support that syntax? Reading the help string, there's a x::Int or _::Int syntax which i assume was intended to support this... I'm assuming this is a generalization of the Foo(x,y) struct destructuring matching... Weird.

Sooo anyway, you're right that type params aren't supported, but i'm still not sure whether we intend to support the syntax as you've written it?

@ghost ghost added bug Something isn't working question Further information is requested labels Jan 2, 2020
@ghost
Copy link

ghost commented Jan 2, 2020

More brokenness:

julia> @match Val(2) begin
           _::Val{2} => "hi"
           _::Val{x} => "bye"
       end
"hi"

julia> @match Val(3) begin
           _::Val{2} => "hi"
           _::Val{x} => "bye"
       end
ERROR: UndefVarError: x not defined

@nystrom
Copy link
Member

nystrom commented Apr 21, 2020

@rai-nhdaly FYI, you cannot use a type as a pattern:

@match 2 begin
   Int => "hi"
   x => 'bye"
end

binds 2 to the variable Int, it doesn't check that type at all. The pattern you want is _::Int.

That said, Rematch should allow type variables to be bound in where clauses, mirroring the function syntax. Something like:

@match Val(3) begin
   _::Val{x} where x => @assert x == 3
end

and

@match [1,2,3] begin
   _::Vector{T} where T => @assert T == Int
end

If you write this now you get:

julia> @match [1,2,3] begin
          _::Vector{T} where T => @assert T == Int
       end
ERROR: UndefVarError: T not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants