Hi,
I am trying to store multiple values in a struct. I’d like those values to have the same precision (expected use cases are floating point and fixed point types with several precisions). Some of those values may be complex, but don’t have to be.
I am basically trying something like
struct MyData{T<:Number}
a::T
b::real(T)
end
or
struct MyData{T} where {T<:Number}
a::T
b::real(T)
end
With the first version, I see the error MethodError: no method matching real(::TypeVar), whereas the second version shows me a syntax error in the (here) first line.
The thing is, I do not want to make a a Complex{T} as in this question and be done with it. Further downstream, I have to do differing things for real T and complex T.
is not an option, because I can’t construct a Complex from another Complex, only from Reals.
How can I store real values and probably real, probably complex values with the same underlying type, so that their type (precision as well as real/complex nature) is preserved for later dispatch?
struct MyData{S, T<:Union{S, Complex{S}}}
a::S # Real
b::T # Real or Complex
end
data1 = MyData(1.0, 2.0)
data2 = MyData(1.0, 2.0+1.0im)
data3 = MyData(1, 2)
data4 = MyData(1, 2-2im)
dispatch(x::MyData{Float64, Float64}) =
x == data1
dispatch(x::MyData{Float64, Complex{Float64}}) =
x == data2
dispatch(x::MyData{Int64, T}) where {T<:Union{Int64, Complex{Int64}}} =
x == data3 || x == data4
# or the last can be done this way
const MaybeComplexInt64 =
Union{Int64, Complex{Int64}}
dispatch(x::MyData{Int64, T}) where {T<:MaybeComplexInt64} =
x == data3 || x == data4
julia> dispatch(data1)
true
julia> dispatch(data2)
true
julia> dispatch(data3)
true
julia> dispatch(data4)
true
Thanks for the hints! I’d prefer dispatching, as I believe this to be faster and I know, that this happens in hot sections.
Do you happen to have any examples how dispatching looks with the ComputedFieldTypes.jl library? I didn’t find anything on their repo after a first glance.