Dispatch on type only

Is it possible to dispatch on a type only, not having an instance with a type?
This works:

function choose(t::Float64)
    println("choosing t::Float64")
end
function choose(t::Tuple)
    println("choosing t::Tuple")
end

tup = (1,2)
choose(tup)

flt = randn()
choose(flt)

For a streaming IO application, I would like to use

choose(Tuple)

but did not get anywhere.

I believe Type{T} is what you’re looking for:

julia> choose(t::Type{Float64}) = println("choosing t::Float64")
choose (generic function with 1 method)

julia> choose(t::Type{Tuple}) = println("choosing t::Tuple")
choose (generic function with 2 methods)

julia> choose(Float64)
choosing t::Float64

julia> choose(Tuple)
choosing t::Tuple
7 Likes

Yess!
I take from this that you always pass something, here t is a Type.

1 Like

The t itself isn’t needed in the signature, you can write just

choose(::Type{Float64}) = ... 
4 Likes

Whether you give it a name in the signature or not, you obviously need to pass something (if you have multiple methods).