# Operations on type paramteres

**URL:** <https://discourse.julialang.org/t/operations-on-type-paramteres/78954>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 3, 2022, 4:58pm UTC](https://discourse.julialang.org/t/operations-on-type-paramteres/78954 "2022-04-03T16:58:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohamed\_AbdElrahman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed_abdelrahman/32/8112_2.png) [@Mohamed\_AbdElrahman](https://discourse.julialang.org/u/Mohamed_AbdElrahman)\
**Post date:** [April 3, 2022, 4:58pm UTC](https://discourse.julialang.org/t/operations-on-type-paramteres/78954/1 "2022-04-03T16:58:41Z")

</div>

Is there is an easy way to do something like this

```julia
struct TensorField{T,ND,NC} <: AbstractArray{T,ND+NC}
end

```

I need to define a scaler/vector/tensor field in 1D/2D/3D and I don’t want to define a type for each dimension.

---

<div class="post-metadata">

**Author:** ![nchisholm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nchisholm/32/37414_2.png) [@nchisholm](https://discourse.julialang.org/u/nchisholm)\
**Post date:** [April 3, 2022, 5:43pm UTC](https://discourse.julialang.org/t/operations-on-type-paramteres/78954/2 "2022-04-03T17:43:05Z")

</div>

My guess is you are trying to indicate the number of covariant and contravariant indices/dimensions with `ND` and `NC`, and they must add up to the total number of array dimensions `N`. You can’t add numeric type variables in type definitions, but you can add their concrete values inside type constructors. Thus, something like the following should work.

```julia
struct TensorField{T,ND,NC,N} <: AbstractArray{T,N}
   ...
   function TensorField{T,ND,NC} where {T,ND,NC}
       ...
       new{T, ND, NC, ND+NC}(...)
   end
end

```

This should essentially enforce the invariant that `ND+NC == N` for any instance of `TensorField`. You could alternatively use an outer constructor and include a check in the inner constructor that `ND+NC == N` or else raise an exception.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 3, 2022, 7:46pm UTC](https://discourse.julialang.org/t/operations-on-type-paramteres/78954/3 "2022-04-03T19:46:16Z")

</div>

> [@Mohamed\_AbdElrahman](#):
>
> `struct TensorField{T,ND,NC} <: AbstractArray{T,ND+NC}`

You can’t do this yet, but there have been discussions of supporting it someday: [Proposal: Defer calculation of field types until type parameters are known · Issue #18466 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/18466)
