# Dynamic creation of similar structs

**URL:** <https://discourse.julialang.org/t/dynamic-creation-of-similar-structs/20247>\
**Category:** General Usage\
**Tags:** question, data\_structures\
**Created:** [January 29, 2019, 9:25pm UTC](https://discourse.julialang.org/t/dynamic-creation-of-similar-structs/20247 "2019-01-29T21:25:21Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [January 29, 2019, 9:25pm UTC](https://discourse.julialang.org/t/dynamic-creation-of-similar-structs/20247/1 "2019-01-29T21:25:21Z")

</div>

Julia does not permit such syntax but what I want is basically

```julia
struct A{T, 2}
    x::T
    y::T 
end

struct A{T, 3}
    x::T
    y::T
    z::T
end

struct A{T, 4}
    x::T
    y::T
    z::T
    w::T
end

```

i.e., the number in type parameter matches the number of fields. The fields could contain `x, y, z, w, a, b, c, d...`, and so on.  
Is there a way of doing this?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [January 29, 2019, 9:28pm UTC](https://discourse.julialang.org/t/dynamic-creation-of-similar-structs/20247/2 "2019-01-29T21:28:20Z")

</div>

A FieldVector comes close  
[http://juliaarrays.github.io/StaticArrays.jl/stable/pages/api.html#FieldVector-1](http://juliaarrays.github.io/StaticArrays.jl/stable/pages/api.html#FieldVector-1)

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [January 29, 2019, 9:31pm UTC](https://discourse.julialang.org/t/dynamic-creation-of-similar-structs/20247/3 "2019-01-29T21:31:26Z")

</div>

Thank you, I know `FieldVector`, but what I am concerned here is that why I should have

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

struct A3{T}
    x::T
    y::T
    z::T
end

struct A4{T}
    x::T
    y::T
    z::T
    w::T
end

```

rather than generating the fields dynamically? The `A`s are closely related and I do not want 3 types. Sorry, I should have been clearer.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 29, 2019, 9:35pm UTC](https://discourse.julialang.org/t/dynamic-creation-of-similar-structs/20247/4 "2019-01-29T21:35:22Z")

</div>

Why does it have to be fields? Maybe a single (named) tuple as a replacement is fine? You could even overwrite `getproperty` to make it feel like fields.

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [January 29, 2019, 9:43pm UTC](https://discourse.julialang.org/t/dynamic-creation-of-similar-structs/20247/5 "2019-01-29T21:43:56Z")

</div>

That’s a good idea. But I am just curious about whether Julia provides us such capability.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [January 29, 2019, 9:49pm UTC](https://discourse.julialang.org/t/dynamic-creation-of-similar-structs/20247/6 "2019-01-29T21:49:12Z")

</div>

You can overload `getproperty` to get the behavior you like without enumerating types.

```julia
julia> struct A{N, Ttuple <: NTuple{N}}
               t::Ttuple
       end

julia> function Base.getproperty(a::A{N}, s::Symbol) where {N}
               s === :x && N >= 1 && return getfield(a, :t)[1]
               s === :y && N >= 2 && return getfield(a, :t)[2]
               s === :z && N >= 3 && return getfield(a, :t)[3]
               s === :w && N >= 4 && return getfield(a, :t)[4]
               return getfield(a, s)
       end

julia> a = A((1,2,3,4));

julia> a.x
1

julia> a.y
2

julia> a.z
3

julia> a.w
4

julia> a = A((1,2,3));

julia> a.w
ERROR: type A has no field w
Stacktrace:
 [1] getproperty(::A{3,Tuple{Int64,Int64,Int64}}, ::Symbol) at .\REPL[45]:6

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 30, 2019, 9:29am UTC](https://discourse.julialang.org/t/dynamic-creation-of-similar-structs/20247/7 "2019-01-30T09:29:34Z")

</div>

> [@singularitti](#):
>
> generating the fields dynamically?

Do you want to generate the _values_ dynamically, or the valid field names?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 30, 2019, 9:49am UTC](https://discourse.julialang.org/t/dynamic-creation-of-similar-structs/20247/8 "2019-01-30T09:49:48Z")

</div>

Why would you do this and not use a `NamedTuple`?

```julia
julia> a = (x=1, y=2, z=3)
(x = 1, y = 2, z = 3)

julia> a.x
1

julia> a.y
2

julia> a.z
3

julia> a.w
ERROR: type NamedTuple has no field w
Stacktrace:
 [1] getproperty(::Any, ::Symbol) at .\sysimg.jl:18

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [January 30, 2019, 10:02am UTC](https://discourse.julialang.org/t/dynamic-creation-of-similar-structs/20247/9 "2019-01-30T10:02:33Z")

</div>

Pure laziness to type `x = `, `y = ` etc.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 30, 2019, 10:12am UTC](https://discourse.julialang.org/t/dynamic-creation-of-similar-structs/20247/10 "2019-01-30T10:12:43Z")

</div>

You can easily create a “constructor”,

```julia
julia> function A(v)
          k = (:x, :y, :z, :w)
          NamedTuple{k[eachindex(v)]}(v)
       end
A (generic function with 1 method)

julia> a = A((1,2,3))
(x = 1, y = 2, z = 3)

julia> a = A((1,2,3,4))
(x = 1, y = 2, z = 3, w = 4)

```

EDIT: ~~`A(v)` isn’t type stable here which could easily be healed if we restrict the input to be `Tuple`s.~~

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [January 30, 2019, 1:10pm UTC](https://discourse.julialang.org/t/dynamic-creation-of-similar-structs/20247/11 "2019-01-30T13:10:58Z")

</div>

I want to generate the valid field names dynamically.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 30, 2019, 1:24pm UTC](https://discourse.julialang.org/t/dynamic-creation-of-similar-structs/20247/12 "2019-01-30T13:24:55Z")

</div>

If you want the `a.b` syntax, you should probably define methods for `Base.propertynames` and `Base.getproperty`. See [the docs](https://docs.julialang.org/en/v1/base/base/#Base.getproperty).
