How to extract type parameters that are used as upper bounds in `UnionAll`?

MWE:

julia> const DictPar{T, V} = Dict{T, <:V}
Dict{T, <:V} where {T, V}

julia> getPar(::Type{D}) where {T, V, D<:DictPar{T, V}} = (T, V)
getPar (generic function with 1 method)

julia> d = DictPar{Int, Real}
Dict{Int64, <:Real}

julia> getPar(d)
ERROR: UndefVarError: `V` not defined in static parameter matching
Suggestion: run Test.detect_unbound_args to detect method arguments that do not fully constrain a type parameter.

Is there a workaround to get V, assuming I don’t have access to instances of d but only d itself?

Thanks!

I don’t think this is possible to achieve in a supported way. Some previous discussion:

AFAIK, in Julia’s dispatch logic, a method static parameter must be uniquely determined, with it’s lower bound equal to its upper bound, to be defined in the method body. So “extracting an upper bound” seems like something that would require additional language features.

2 Likes