# What is the triangle in "triangular dispatch"?

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?

3 Likes

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.)

2 Likes

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?

2 Likes

It should be:

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

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

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

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

julia> g(Unsigned, Unsigned(1))  # Vector{S} S
"dispatch!"
``````
5 Likes