Confused about dispatch on type


#1

Hi,

I am playing with MLDataPattern and I am confused with use of ObservationDimension. The problem is that I cannot use it inside the definition of a type in function. To be clear, I have replicated that in this toy example:

immutable Constant{DIM} end
Constant(dim::Int) = Constant{dim}()
function x(c::A) where {A::Constant{1}}
println(“1”)
end

The compiler returns an error

ERROR: syntax: invalid variable expression in “where”

Even though

isa(Constant(1),Constant{1})
this works properly.

What am I doing wrong?

Thanks for help.


#2

Just use

function x(c::Constant{1})

as there’s no need for the where notation here. If you did wanna use that then the correct incantation is

function x(c::T) where T<:Constant{1}

Notice <: (which defines a subtyping relation) instead of :: (which defines an isa relation). The former doesn’t really make sense for concrete types though.


#3

Thank you very much. It works!!