# Algebraic constraints for type parameters

**URL:** <https://discourse.julialang.org/t/algebraic-constraints-for-type-parameters/6409>\
**Category:** General Usage\
**Created:** [October 12, 2017, 12:49pm UTC](https://discourse.julialang.org/t/algebraic-constraints-for-type-parameters/6409 "2017-10-12T12:49:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Post date:** [October 12, 2017, 12:49pm UTC](https://discourse.julialang.org/t/algebraic-constraints-for-type-parameters/6409/1 "2017-10-12T12:49:36Z")

</div>

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:

```julia
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

```julia
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?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [October 12, 2017, 12:57pm UTC](https://discourse.julialang.org/t/algebraic-constraints-for-type-parameters/6409/2 "2017-10-12T12:57:47Z")

</div>

No, see [https://github.com/JuliaLang/julia/issues/18466](https://github.com/JuliaLang/julia/issues/18466) and [GitHub - vtjnash/ComputedFieldTypes.jl: Build types in Julia where some fields have computed types](https://github.com/vtjnash/ComputedFieldTypes.jl) .
