How to do arithmetic in the type system?

In addition to what others have said, you can mostly achieve what you’re looking for with an extra type parameter, but it is inconvenient:

struct MyStruct{L,P, N}
    a::SVector{L,Other1}
    b::SVector{P,Other2}
    c::SVector{N,Float64}

    function MyStruct{L, P, N}(...)
        # Check that N == L + P here
        ....
    end
end


# Convenient outer constructor that computes N for you
function MyStruct{L, P}(...) where {L, P}
    MyStruct{L, P, L + P}(...)
end
5 Likes