Separate dispatch on Type{T} for subtypes

question

#1

Consider the following:

abstract A
type B <: A end
type C <: A end

Base.show(io::IO, ::MIME"text/plain", ::Type{A}) = print(io, "I'm abstract type A")
Base.show{T<:A}(io::IO, ::MIME"text/plain", ::Type{T}) = print(io, "I'm type $T, a subtype of A")

The first show method is not called for A:

julia> B
I'm type B, a subtype of A

julia> A
I'm type A, a subtype of A

Is it possible to dispatch separately for A and B?


#2

Wait for 0.6 to come out? The type system revamp in 0.6 enables the first method to be considered more specific for A. I think the only way to do this in 0.5 is with a branch inside the function body:

Base.show{T<:A}(io::IO, ::MIME"text/plain", ::Type{T}) = T === A ? print(io, "I'm abstract type A") : print(io, "I'm type $T, a subtype of A")

Since Julia still specializes on the exact type, the branch is completely removed when the method is compiled.


#3

Thanks Matt!