# Is it possible to have a typevariable for an upperbound?

**URL:** https://discourse.julialang.org/t/is-it-possible-to-have-a-typevariable-for-an-upperbound/35274
**Category:** General Usage
**Tags:** question
**Created:** [February 28, 2020, 7:22am UTC](https://discourse.julialang.org/t/is-it-possible-to-have-a-typevariable-for-an-upperbound/35274 "2020-02-28T07:22:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [February 28, 2020, 7:22am UTC](https://discourse.julialang.org/t/is-it-possible-to-have-a-typevariable-for-an-upperbound/35274/1 "2020-02-28T07:22:34Z")

</div>

Just stumbled upon that I would like to transform a specific container to another specific container, and it looks like I have to take special care about Julia’s UnionAll types.  
Concretely, I would like to map say `Vector{<:Number}` to `MyVector{<:Number}`.

What I thought could do the trick is a function like the following

```julia
f(::Type{Vector{<:T}}) where T = T

```

however

```julia
f(Vector{<:Number})

```

raises the error `ERROR: UndefVarError: T not defined`

Is something like this not supported in Julia? What could be a workaround?  
thanks a lot

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [February 29, 2020, 2:39pm UTC](https://discourse.julialang.org/t/is-it-possible-to-have-a-typevariable-for-an-upperbound/35274/2 "2020-02-29T14:39:36Z")

</div>

You’ll have to handle it specially, but see `Base.parameter_upper_bound`.

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [March 1, 2020, 8:07am UTC](https://discourse.julialang.org/t/is-it-possible-to-have-a-typevariable-for-an-upperbound/35274/3 "2020-03-01T08:07:29Z")

</div>

I build my own small solution to enable dispatch on UnionAll types.

first we need a little wrapper around TypeVariable which captures the bounds information in type-parameters

```julia
struct TV{LB, UB}
  lb::Type{LB}
  name::Symbol
  ub::Type{UB}
end
TV(tv::TypeVar) = TV(tv.lb, tv.name, tv.ub)
# for convenient syntax as upperbound is most often what you want to dispatch on
const TV′{UB, LB} = TV{LB, UB} 

```

Then 3 little functions do the trick to make the typeparameters available for dispatch on respective extra arguments to the function `f`

```julia
f(T::UnionAll) = _f(T, T.body, TV(T.var))
_f(T, S::UnionAll, vs...) = _f(T, S.body, vs..., TV(S.var))
_f(T, S::DataType, vs...) = f(T, vs...)

```

Now we can define dispatch rules like the following

```julia
f(::Type{<:Vector}, ::TV′{T}) where T <: Number = T
f(::Type{<:Vector}, ::TV) = true
f(::Type{<:Tuple}, ::TV′{Number}, ::TV′{AbstractString}) = "hi"

```

which indeed work like wanted

```julia
f(Vector{<:Number}) # Number
f(Vector{<:Integer}) # Integer
f(Vector) # true
f(Tuple{<:Number, <:AbstractString}) # "hi"

```
