I have a structure with a floating point type parameter for the arrays and I want to include a StepRangeLen with fitting type. How can this be achieved?
struct MyType{S<:Union{Float32, Float64}}
data::Vector{S}
grid::StepRangeLen of returntype S
end
For S == Float32 the corresponding type would be StepRangeLen{Float32, Float64, Float64, Int64}
but for S == Float64 it is StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}
Has anyone a idea how to correctly parameterize the Range given the basetype S?
It’s generally waaaay easier to ensure invariants through constructor methods than type parameter constraints. Yes, it’s not “statically enforced” but neither is Julia itself.
struct MyType{D, G}
data::D
grid::G
end
function MyType(data, grid)
eltype(data) === eltype(grid) || throw(ArgumentError("blah"))
# more checks as necessary
return MyType{typeof(data), typeof(grid)}(data, grid)
end