# How do I concretely type a field in a struct dynamically?

**URL:** https://discourse.julialang.org/t/how-do-i-concretely-type-a-field-in-a-struct-dynamically/94524
**Category:** General Usage
**Created:** [February 12, 2023, 5:57pm UTC](https://discourse.julialang.org/t/how-do-i-concretely-type-a-field-in-a-struct-dynamically/94524 "2023-02-12T17:57:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [February 12, 2023, 5:57pm UTC](https://discourse.julialang.org/t/how-do-i-concretely-type-a-field-in-a-struct-dynamically/94524/1 "2023-02-12T17:57:40Z")

</div>

Hello!

For performance reasons I have to concretely type this field in my struct;

```julia
struct Test{I,D}
    Stencil::Vector{Tuple{repeat([I],D)...}}
end

```

I’ve tried to do as follows but it errors out. “I” is the type I wish to use, in this case Int, and “D” is the dimensions, for example two or three. In this case I would like it to produce, dynamically:

```julia
Stencil::Vector{Tuple{I,I}}
# or
Stencil::Vector{Tuple{I,I,I}}

```

**The type has to be concretely typed as this** else I get a 100x performance penalty in my specific code.

Can one do this or do I need macro’s?

Kind regards

---

<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: [February 12, 2023, 6:07pm UTC](https://discourse.julialang.org/t/how-do-i-concretely-type-a-field-in-a-struct-dynamically/94524/2 "2023-02-12T18:07:04Z")

</div>

Might `NTuple{D, I}` be the type you are looking for?

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [February 12, 2023, 6:12pm UTC](https://discourse.julialang.org/t/how-do-i-concretely-type-a-field-in-a-struct-dynamically/94524/3 "2023-02-12T18:12:30Z")

</div>

Indeed I was!

Thank you 🙂

I found this out just a minute after your reply. I had to wrangle it out of ChatGPT haha. Here conversation is included for fun!

> **Me** : Hello! For performance reasons I have to concretely type this field in my struct; struct Test{I,D} Stencil::Vector{Tuple{repeat([I],D)…}} end I’ve tried to do as follows but it errors out. “I” is the type I wish to use, in this case Int, and “D” is the dimensions, for example two or three. In this case I would like it to produce, dynamically: Stencil::Vector{Tuple{I,I}} # or Stencil::Vector{Tuple{I,I,I}} The type has to be concretely typed as this else I get a 100x performance penalty in my specific code. Can one do this or do I need macro’s? Kind regards

> **ChatGPT** : Yes, you can achieve this in Julia by using type parameters and constructing the type inside the struct. Here’s an example:

```julia
struct Test{I, D}
    vec::Stencil.Vector{Tuple{repeat(I, D)...}}
end

julia> Test{Int, 2}
Test{Int64,2}

julia> Test{Int, 3}
Test{Int64,3}

```

> In this example, `I` is the type parameter that you want to use (e.g., `Int`), and `D` is the number of dimensions (e.g., 2 or 3). The `repeat` function is used to repeat the type `I` `D` times, and the resulting type is `Tuple{I, I}` or `Tuple{I, I, I}`, depending on the value of `D`. This type is then used as the type parameter for the `vec` field of the `Test` struct.

> **Me:** Stencil is the name of the field, not a function.

> **ChatGPT:** My apologies, I misunderstood the name of the field. Here’s an updated version of the code that correctly uses `Stencil` as the field name:

```julia
struct Test{I, D}
    Stencil::Stencil.Vector{Tuple{repeat(I, D)...}}
end

julia> Test{Int, 2}
Test{Int64,2}

julia> Test{Int, 3}
Test{Int64,3}

```

> **Me:** No you are still doing it the wrong. This is the correct version: struct Test{I,D} Stencil::Vector{Tuple{repeat([I],D)…}} end But this code gives an error

> **ChatGPT:** Ah, I apologize for the mistake. The correct version is indeed as you wrote:

```julia
struct Test{I,D}
    Stencil::Vector{Tuple{repeat([I],D)...}}
end

```

> In this case, `repeat` takes two arguments: the first one is `[I]`, which creates an array with a single element `I`, and the second argument is `D`, which specifies the number of times to repeat `[I]`. The `...` operator then spreads the elements of the repeated array into a tuple, creating `Tuple{I,I,...,I}` with `D` elements.

> **Me:** Yes, but this cannot be called in a type declaration?

> **ChatGPT:** Yes, you are correct. The `repeat` function cannot be used directly in a type declaration. To construct a type with repeated elements, you can use a combination of type parameters and type composition. Here’s an example that demonstrates how to achieve this:

```julia
struct Test{I,D}
    Stencil::Vector{NTuple{D, I}}
end

```

> In this example, `NTuple{D, I}` is used to create a `Tuple` with `D` elements, all of type `I`. The type parameter `D` allows you to specify the number of dimensions, and `I` allows you to specify the type.

```julia
julia> Test{Int, 2}
Test{Int64,2}

julia> Test{Int, 3}
Test{Int64,3}

```

**Just included for fun 🙂**
