Parametric type with numbers

Hello!

I am having difficulties trying to model the adequate code for what I want to achieve.
Imagine I have the following struct:

struct Device
    a::NTuple{3,Float64}
    b::NTuple{2,Float64}
end

where b is always going to be one element shorter than a. How can I generalize this for N?

I figured that it would be something like

struct Device{N}
    a::NTuple{N,Float64}
    b::NTuple{N-1,Float64}
end

but it errors because I cannot perform substraction on types.

Thanks!

1 Like

I think Matt’s answer here is still right:

https://stackoverflow.com/questions/34073243/expressions-depending-on-integer-type-parameters-in-type-definitions-in-julia-ar

5 Likes

There is GitHub - vtjnash/ComputedFieldTypes.jl: Build types in Julia where some fields have computed types which helps with this. There is also a github issue around this https://github.com/JuliaLang/julia/issues/18466

1 Like

Thank you both!!

1 Like