Open MasonProtter opened 4 years ago
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?
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
@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
Rematch can't handle pattern matching on parametric types: