lrnv
1
Hey,
I have a method :
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:
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.
You just want where T <: Union{T1,T2}
. Concrete types can’t have subclasses.
lrnv
3
Aaand you are right. Thanks
DNF
4
Or just
function f(x::Union{T1,T2})
# stuff
end
2 Likes
lrnv
5
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.
DNF
6
Yes. It just wasn’t necessary in your particular example, so I wanted to point it out.
1 Like