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

**URL:** https://discourse.julialang.org/t/fr-a-new-variant-on-method-static-parameter-matching-for-getting-the-upper-bound/118261
**Category:** Internals & Design
**Tags:** parametric-types, feature-request, parameters
**Created:** [August 16, 2024, 9:16am UTC](https://discourse.julialang.org/t/fr-a-new-variant-on-method-static-parameter-matching-for-getting-the-upper-bound/118261 "2024-08-16T09:16:48Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [August 16, 2024, 9:16am UTC](https://discourse.julialang.org/t/fr-a-new-variant-on-method-static-parameter-matching-for-getting-the-upper-bound/118261/1 "2024-08-16T09:16:48Z")

</div>

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:

> <https://github.com/JuliaLang/julia/blob/5230d27de950165475892acfabded59713c8cd3e/base/abstractarray.jl#L242-L242>

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-repl
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:

> [@Is there a way to capture an upper bound via dispatch?](https://discourse.julialang.org/t/is-there-a-way-to-capture-an-upper-bound-via-dispatch/63718):
>
> As part of the logic of writing some function, I’d like to use the upper bound of some passed-in type’s argument. Is there any way to capture this upper bound cleanly via dispatch? Basically, I’d like the following: struct Foo{T\<:Real} end capture\_upper\_bound(Foo{Real}) # should return Real capture\_upper\_bound(Foo{\<:Real}) # should return Real capture\_upper\_bound(Foo{\<:Int}) # should return Int capture\_upper\_bound(Foo{\<:Number}) # should be a method error The following definition of c…

> [@Deconstructing UnionAll types](https://discourse.julialang.org/t/deconstructing-unionall-types/108328):
>
> The following naive definition is invalid, but you don’t realize it until you try it: julia\> f(::Type{Set{\<:W}} where W) = W f (generic function with 4 methods) Here goes… julia\> f(Set{\<:Integer}) ERROR: UndefVarError: `W` not defined I can get the functionality I wanted with a hack: julia\> f\_hack(t::Type{Set{\<:W}} where W) = t.var.ub f\_hack (generic function with 1 method) julia\> f\_hack(Set{\<:Integer}) Integer So how do I do this with public API?

> [@How to extract type parameters that are used as upper bounds in \`UnionAll\`?](https://discourse.julialang.org/t/how-to-extract-type-parameters-that-are-used-as-upper-bounds-in-unionall/117557):
>
> 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 insta…
