Algebraic constraints for type parameters


#1

Hello.
Let’s say I want to create a type which stores two tuples: one of them is exactly as twice as large as the other one:

struct MyType{D}
    t1::NTuple{D, Int64}
    t2::NTuple{2*D,Int64}
end

This code obviously is not working as there is no method *(::Int64,::TypeVar).
To overcome this I can think of something like

struct MyType{D1,D2}
    t1::NTuple{D1,Int64}
    t2::NTuple{D2,Int64}
    function MyType(t1::NTuple{D1,Int64}, t2::NTuple{D2,Int64}) where {D1,D2}
        d1 = length(t1)
        d2 = length(t2)
        assert(d2 == d1*2)
        new{D1,D2}(t1,t2)
     end
end

But is there a better way to impose algebraic constraints on type parameters?


NTuple length dependent on parameter of type
#2

No, see https://github.com/JuliaLang/julia/issues/18466 and https://github.com/vtjnash/ComputedFieldTypes.jl .