What is the triangle in "triangular dispatch"?


#1

More than 5 year ago Tim Holey wished he could write

colorspace{C<:ColorValue,A<:AbstractArray{C}}(img::A) = string(C)

He called such a feature “triangular dispatch (left-to-right template parameter chaining)”.

Today Julia does support such “triangular” dispatch and Tim’s example can be written as

colorspace(img::A) where A<:AbstractArray{C} where C<:ColorValue = string(C)

I understand the “left-to-right template parameter chaining” part, but what makes this kind of dispatch triangular? What are the vertices of that triangle?


#2

The ability to define a function g{T, S<:T}(::Vector{T}, ::S) has been referred to as “triangular dispatch” as an analogy to diagonal dispatch: f{T}(::Vector{T}, ::T) . (Imagine a table with a type hierarchy labelling the rows and columns, arranged such that the super types are to the top and left. The rows represent the element type of the first argument, and the columns the type of the second. Diagonal dispatch will only match the cells along the diagonal of the table, whereas triangular dispatch matches the diagonal and everything below it, forming a triangle.)


#3

Edit: the following is wrong. It is backwards. See the correct version below this entry.

@Gnimuc, I interpret that to mean
R <: S <: T

Triangular:

          | T | S | R 
----------+---+---+---
Vector{T} | X |   |
Vector{S} | X | X |
Vector{R} | X | X | X

Diagonal:

          | T | S | R 
----------+---+---+---
Vector{T} | X |   |
Vector{S} |   | X |
Vector{R} |   |   | X

Where X marks the combination that will cause the function g{T, R<:T}(::Vector{T}, ::R) to be dispatched.

Correct?


#4

It should be:

T S
Vector{T} :white_check_mark: :white_check_mark:
Vector{S} x :white_check_mark:
julia> g(t::Vector{T}, s::S) where {T, S<:T} = "dispatch!"

julia> g(Integer[1], Integer(1))  # Vector{T} T
"dispatch!"

julia> g(Integer[1], Unsigned(1))  # Vector{T} S
"dispatch!"

julia> g(Unsigned[1], Integer(1))  # Vector{S} T
ERROR: MethodError: no method matching g(::Array{Unsigned,1}, ::Int64)

julia> g(Unsigned[1], Unsigned(1))  # Vector{S} S
"dispatch!"