# Restricting method dispatch to a few concrete types

**URL:** https://discourse.julialang.org/t/restricting-method-dispatch-to-a-few-concrete-types/63765
**Category:** New to Julia
**Created:** [June 29, 2021, 3:26pm UTC](https://discourse.julialang.org/t/restricting-method-dispatch-to-a-few-concrete-types/63765 "2021-06-29T15:26:39Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [June 29, 2021, 3:26pm UTC](https://discourse.julialang.org/t/restricting-method-dispatch-to-a-few-concrete-types/63765/1 "2021-06-29T15:26:39Z")

</div>

Hey,

I have a method :

```julia
function f(x)
    return x
end

```

for which i have a faster version that works only if x is either of type T1 or of type T2. I tried writting:

```julia
function f(x::T) where {Union{T1,T2} <: T <: Union{T1,T2}}
    //stuff
end

```

but it did not work.

How can i specify that the Type must be either T1 or T2 ? (it cannot be any subtypes of T1 and T2, which are already concretes types).

The specific usecase is `T1,T2 = Float32,Float64`, for wich the function directly calls into lapack.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [June 29, 2021, 3:38pm UTC](https://discourse.julialang.org/t/restricting-method-dispatch-to-a-few-concrete-types/63765/2 "2021-06-29T15:38:48Z")

</div>

You just want ` where T <: Union{T1,T2}`. Concrete types can’t have subclasses.

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [June 29, 2021, 3:39pm UTC](https://discourse.julialang.org/t/restricting-method-dispatch-to-a-few-concrete-types/63765/3 "2021-06-29T15:39:59Z")

</div>

Aaand you are right. Thanks

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [June 29, 2021, 3:46pm UTC](https://discourse.julialang.org/t/restricting-method-dispatch-to-a-few-concrete-types/63765/4 "2021-06-29T15:46:32Z")

</div>

Or just

```julia
function f(x::Union{T1,T2}) 
    # stuff
end

```

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [June 29, 2021, 3:50pm UTC](https://discourse.julialang.org/t/restricting-method-dispatch-to-a-few-concrete-types/63765/5 "2021-06-29T15:50:26Z")

</div>

Yep but does not work in my case since many parameters use the `T` parametric type, and I want it to be the same for everyone.

Just to note that :

`function f(x::Union{T1,T2}, y::Union{T1,T2}) where {T1,T2}`  
is not the same as  
`function f(x::T, y::T) where {T<:Union{T1,T2}}`.

In the first case `x` and `y` might not be the same type, and in the second they are guarenteed to be the same type.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [June 29, 2021, 4:04pm UTC](https://discourse.julialang.org/t/restricting-method-dispatch-to-a-few-concrete-types/63765/6 "2021-06-29T16:04:34Z")

</div>

Yes. It just wasn’t necessary in your particular example, so I wanted to point it out.
