# Compile-time arithmetic for parameterized types

**URL:** <https://discourse.julialang.org/t/compile-time-arithmetic-for-parameterized-types/13991>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [August 24, 2018, 12:21pm UTC](https://discourse.julialang.org/t/compile-time-arithmetic-for-parameterized-types/13991 "2018-08-24T12:21:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [August 24, 2018, 12:21pm UTC](https://discourse.julialang.org/t/compile-time-arithmetic-for-parameterized-types/13991/1 "2018-08-24T12:21:58Z")

</div>

I want to define type like this:

```julia
struct A{L}
  x::NTuple{L,Int}; y::NTuple{L^2,Int}
end

```

Here `L` is known at compile-time. Therefore `L^2` could also be known at compile-time in principle. But the example above is a syntax error. I was forced to do this:

```julia
struct A{L,L2}
  x::NTuple{L,Int}; y::NTuple{L2,Int}
  function A{L,L2}(x::NTuple{L,Int}, y::NTuple{L2,Int}) where {L,L2}
     @assert L2 == L^2
     new{L,L2}(x,y)
  end
end

```

But this ends up computing the square of `L` at runtime (when I check for `L2 == L^2`). I would like to avoid this runtime calculation, since `L` is known at compile-time. Moreover, I don’t want to let `L,L2` be independent values in `A{L,L2}`, because having a `A{L,L2}` with `L2!=L^2` would be an invalid state. Therefore the check is necessary, but ideally it should be a compile-time check.

Is there a better way to do this kind of thing?

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [August 24, 2018, 12:36pm UTC](https://discourse.julialang.org/t/compile-time-arithmetic-for-parameterized-types/13991/2 "2018-08-24T12:36:44Z")

</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)
