Dispatching on values with constraints

Within a where expression, I can create upper bounds on types with <:

Array{T} where {T<:Real}

I am just wondering if it will ever be possible to use :: or isa within where expressions to dispatch on type parameter values, like

Val{T} where {T::Real}
# or, perhaps
Val{T} where {T isa Real}

so that, e.g., Val(1) <: Val{T isa Real}. I suppose it just might not ever be possible with the type system and I probably need to use WhereTraits.jl / SimpleTraits.jl. (Traits would also be nice for things like f(::Array{T,N}) where {T,N<=2} = ... without needing both Array{T,1} and Array{T,2})

Part of the reason I ask is because types are technically instances of a class DataType:

julia> typeof(Float64)

So, in some ways, T <: Real isn’t too far removed from x < 1.0. Technically, <: is just an operator between two instances T and Real. Obviously on the practical and implementation side, these are very different! But it’s interesting to think about this more generally as where clauses describing a subset of some space, and the dispatch system targetting the method with the smallest volume(/measure) which contains the instance. Right now we are working with a 1D measures along DataType space but perhaps this could be extended to other spaces if there is a hierarchy in place so that smaller measure along one axis takes precedent over a smaller measure along another axis.

Probably also relevant to multiple inheritance: abstract multiple inheritance · Issue #5 · JuliaLang/julia · GitHub



NB: the discussion in that PR is not completely on-topic relatively to the original issue post. The issue was IMO hijacked from the original topic, but in any case the current title and topic corresponds to your question.


Cool! Looks like it’s on the road map which is great. Thanks for the link.

1 Like