# Create parametric structure with metaprogramming

**URL:** <https://discourse.julialang.org/t/create-parametric-structure-with-metaprogramming/77184>\
**Category:** New to Julia\
**Tags:** metaprogramming, parametric-types\
**Created:** [February 28, 2022, 1:37pm UTC](https://discourse.julialang.org/t/create-parametric-structure-with-metaprogramming/77184 "2022-02-28T13:37:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albert\_de\_montserrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_de_montserrat/32/9135_2.png) [@Albert\_de\_montserrat](https://discourse.julialang.org/u/Albert_de_montserrat)\
**Post date:** [February 28, 2022, 1:37pm UTC](https://discourse.julialang.org/t/create-parametric-structure-with-metaprogramming/77184/1 "2022-02-28T13:37:10Z")

</div>

I’m trying to use metraprogramming to create structures on the fly. I managed to make a construtor for a vanilla struct as:

```julia
function makestruct(name::Symbol, ndim::Integer)
    dims = (:x, :y, :z)
    fields = [:( $(dims[i]) ) for i in 1:ndim]
    @eval begin
        struct $(name)
            $(fields...)
        end
    end
end

```

However, I can’t manage to make a parametric struct with metaprogramming. In the end what I would like to simply generate:

```julia
struct bar{T}
    x::T
    y::T
end

```

or

```julia
struct bar{T}
    x::T
    y::T
    z::T
end

```

depending on `ndim`

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 28, 2022, 1:56pm UTC](https://discourse.julialang.org/t/create-parametric-structure-with-metaprogramming/77184/2 "2022-02-28T13:56:47Z")

</div>

It seems like your existing code is already almost there–you just need to add `T` in the appropriate places:

```julia
julia> function makestruct(name::Symbol, ndim::Integer)
           dims = (:x, :y, :z)
           fields = [:( $(dims[i])::T ) for i in 1:ndim]
           @eval begin
               struct $(name){T}
                   $(fields...)
               end
           end
       end
makestruct (generic function with 1 method)

julia> makestruct(:bar, 2)

julia> bar(1, 2)
bar{Int64}(1, 2)

```

---

<div class="post-metadata">

**Author:** ![Albert\_de\_montserrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_de_montserrat/32/9135_2.png) [@Albert\_de\_montserrat](https://discourse.julialang.org/u/Albert_de_montserrat)\
**Post date:** [February 28, 2022, 2:13pm UTC](https://discourse.julialang.org/t/create-parametric-structure-with-metaprogramming/77184/3 "2022-02-28T14:13:43Z")

</div>

Oh thanks 😃 I was unsuccesfuly trying to parse the ::T directly inside the eval call

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 28, 2022, 2:33pm UTC](https://discourse.julialang.org/t/create-parametric-structure-with-metaprogramming/77184/4 "2022-02-28T14:33:51Z")

</div>

By the way, you might be interested in `StaticArrays.FieldVector`, which is designed to let types like this operate as statically sized vectors in a convenient way: [API · StaticArrays.jl](https://juliaarrays.github.io/StaticArrays.jl/stable/pages/api/#FieldVector)

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 28, 2022, 2:35pm UTC](https://discourse.julialang.org/t/create-parametric-structure-with-metaprogramming/77184/5 "2022-02-28T14:35:38Z")

</div>

Oh, and also, if your actual use-case really is small vectors with `x, y, z` labels, note that you can already use `StaticArrays.SVector` for that with no changes at all:

```julia
julia> using StaticArrays

julia> v = SVector{2, Float64}(1.0, 2.0)
2-element SVector{2, Float64} with indices SOneTo(2):
 1.0
 2.0

julia> v.x
1.0

julia> v.y
2.0

```

---

<div class="post-metadata">

**Author:** ![Albert\_de\_montserrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_de_montserrat/32/9135_2.png) [@Albert\_de\_montserrat](https://discourse.julialang.org/u/Albert_de_montserrat)\
**Post date:** [February 28, 2022, 3:02pm UTC](https://discourse.julialang.org/t/create-parametric-structure-with-metaprogramming/77184/6 "2022-02-28T15:02:24Z")

</div>

Thanks for the suggestion, but in this particular case `x, y, z` will either be 2 or 3 dimensional arrays too large for `StaticArrays`.
