[FR] a new variant on method static parameter matching for getting the upper bound

The method static parameter matching we currently have works like so: unless the lower bound and the upper bound on the parameter are equal, the parameter is undefined.

In practice, what we often actually want is just the upper bound, independently of the lower bound, so I think it’d be very useful to introduce a new variant on method static parameter matching that would always result in the upper bound.

Sadly, I have no idea what could the syntax for this feature look like.

Probably non-Type static parameters should result in a method error.

One place where this would clearly be useful is eltype. Take this method, for example:

So the method currently works like this: when the lower and upper bounds on the element type match, return the method static parameter (element type), otherwise return Any. Clearly it’d be just as accurate, but more precise, to always just return the upper bound, as it should be Any in the worst case. The idea is for the requested feature to allow behavior like that.

Specific example:

julia> T = AbstractVector{T} where {T<:Integer}
AbstractVector{T} where T<:Integer (alias for AbstractArray{T, 1} where T<:Integer)

julia> eltype(T)
Any

Ideally the last returned value would be Integer instead of Any. This example is relevant for Base.OneTo, for example.

Examples of threads where this feature is required:

4 Likes