# Operation with struct parameter within struct definition

**URL:** https://discourse.julialang.org/t/operation-with-struct-parameter-within-struct-definition/130261
**Category:** General Usage
**Tags:** question
**Created:** [June 26, 2025, 11:22pm UTC](https://discourse.julialang.org/t/operation-with-struct-parameter-within-struct-definition/130261 "2025-06-26T23:22:15Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![JpDemarchi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpdemarchi/32/217478_2.png) [@JpDemarchi](https://discourse.julialang.org/u/JpDemarchi)
#### Post date: [June 26, 2025, 11:22pm UTC](https://discourse.julialang.org/t/operation-with-struct-parameter-within-struct-definition/130261/1 "2025-06-26T23:22:15Z")

</div>

I want to make a struct where the second parameter used represents the dimensionality of the world, with scalar fields being N-dimensional arrays and vector fields being (N + 1)-dimensional arrays. Something like this:

```julia
struct Domain{T, N}
    scalar_fields::Array{T, N}
    vector_fields::Array{T, N + 1}
end

dom = Domain{Float64, 2}(zeros(Float64, 10, 10), zeros(Float64, 10, 10, 10))

```

This is not allowed since the `+` operation in being done within the struct definition with `N` is still a `TypeVar`, giving the error:

`ERROR: MethodError: no method matching +(::TypeVar, ::Int64)`

I am aware this could be technically accomplished by just omitting the dimension of the `Array` in the `vector_fields` definition and enforcing its dimension through a constructor but since `Array{Float64}` is not a concrete type, I’d rather not pay the performance cost and just hardcode it if it’s not possible.

Is there a way to do this properly?

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [June 26, 2025, 11:28pm UTC](https://discourse.julialang.org/t/operation-with-struct-parameter-within-struct-definition/130261/2 "2025-06-26T23:28:35Z")

</div>

I think the only way to do this to use another parameter,

```julia
struct Domain{T, N, M}
    scalar_fields::Array{T, N}
    vector_fields::Array{T, M}
end

```

and only use it for `M == N+1`. You could add this as a test inside a constructor. Such a test would be evaluated at compile-time, so that it doesn’t incur a runtime penalty.

---

<div class="post-metadata">

### Author: ![JpDemarchi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpdemarchi/32/217478_2.png) [@JpDemarchi](https://discourse.julialang.org/u/JpDemarchi)
#### Post date: [June 26, 2025, 11:30pm UTC](https://discourse.julialang.org/t/operation-with-struct-parameter-within-struct-definition/130261/3 "2025-06-26T23:30:24Z")

</div>

That would work, but hurts my OCD deeply.  
Maybe I’ll end up doing it that way if there isn’t a nicer one so thanks!

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [June 27, 2025, 12:00am UTC](https://discourse.julialang.org/t/operation-with-struct-parameter-within-struct-definition/130261/4 "2025-06-27T00:00:59Z")

</div>

From one of the “Related” threads that Discourse suggested below here:

> [@Arithmetic for parametric type values in structs e.g., N+1](https://discourse.julialang.org/t/arithmetic-for-parametric-type-values-in-structs-e-g-n-1/110925/4):
>
> I would do the following. julia\> struct Foo{N,M} arr1::Array{Float64, N} arr2::Array{Float64, M} Foo{N}(a,b) where N= new{N, N+1}(a,b) end julia\> Foo{1}(rand(2), rand(2,3)) Foo{1, 2}([0.9165330134722103, 0.8133055136507046], [0.15120619990261142 0.8543356012671888 0.5239972992036701; 0.12074486400173157 0.6817097391005016 0.3327645622931258])

Still uses an extra type parameter, but the constructor definition neatly makes it so that the redundant parameter doesn’t have to be explicitly given during struct instance initialization.

Another related thread recommended [GitHub - vtjnash/ComputedFieldTypes.jl: Build types in Julia where some fields have computed types](https://github.com/vtjnash/ComputedFieldTypes.jl)
