# Struct with new instances according to arguments passed

**URL:** <https://discourse.julialang.org/t/struct-with-new-instances-according-to-arguments-passed/59292>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [April 14, 2021, 5:54pm UTC](https://discourse.julialang.org/t/struct-with-new-instances-according-to-arguments-passed/59292 "2021-04-14T17:54:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![deveshjawla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/deveshjawla/32/16048_2.png) [@deveshjawla](https://discourse.julialang.org/u/deveshjawla)\
**Post date:** [April 14, 2021, 5:54pm UTC](https://discourse.julialang.org/t/struct-with-new-instances-according-to-arguments-passed/59292/1 "2021-04-14T17:54:35Z")

</div>

Hello,

I would like to know if there exists any concise solution to create a struct which can be initialised with any number of arguments? like so:

```julia
struct VariableStruct
a
b
c
d
e
f
return new(;a,b,c,d,e,f)
end

```

1. With one argument

So now if I would like to initiate a new instance of `VariableStruct` with only say `e` declared like so:

```julia
myVar= VariableStruct(e=5)

```

then I’d like `myVar` to be initialised as `VariableStruct(nothing, nothing,nothing, nothing,5,nothing)`

1. With say 3 arguments

Initiate a new instance of `VariableStruct` with only say `a,d,e` declared like so:

```julia
myVar = VariableStruct(a=1,d=3,e=5)

```

then I’d like `myVar` to be initialised as `VariableStruct(1, nothing,nothing, 3 ,5 ,nothing)`

Does this functionality exist? Or in other words how to achieve it?  
As you may noticed I used Kwargs, this is an idea I have preemptively tried to put in my question 🙂

---

<div class="post-metadata">

**Author:** ![vavrines](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vavrines/32/8024_2.png) [@vavrines](https://discourse.julialang.org/u/vavrines)\
**Post date:** [April 14, 2021, 5:57pm UTC](https://discourse.julialang.org/t/struct-with-new-instances-according-to-arguments-passed/59292/2 "2021-04-14T17:57:14Z")

</div>

[Parameters.jl](https://github.com/mauro3/Parameters.jl)

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [April 14, 2021, 6:07pm UTC](https://discourse.julialang.org/t/struct-with-new-instances-according-to-arguments-passed/59292/3 "2021-04-14T18:07:38Z")

</div>

Just create a constructor that takes kwargs initialized to `nothing`?

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [April 14, 2021, 6:38pm UTC](https://discourse.julialang.org/t/struct-with-new-instances-according-to-arguments-passed/59292/4 "2021-04-14T18:38:06Z")

</div>

> [@deveshjawla](#):
>
> `VariableStruct(a=1,d=3,e=5)`

This is possible without adding new packages. As @gustaphe said, you can define a new [inner constructor](https://docs.julialang.org/en/v1/manual/constructors/#man-inner-constructor-methods) with default values:

```julia
julia> struct VariableStruct
       a
       b
       c
       d
       e
       f
       VariableStruct(;a = nothing,b = nothing,c = nothing,d = nothing,e = nothing,f= nothing) = new(a, b, c, d, e, f)
       end

julia> VariableStruct()
VariableStruct(nothing, nothing, nothing, nothing, nothing, nothing)

julia> VariableStruct(a = 1)
VariableStruct(1, nothing, nothing, nothing, nothing, nothing)

julia> VariableStruct(a = 1, f = 2)
VariableStruct(1, nothing, nothing, nothing, nothing, 2)

```

---

<div class="post-metadata">

**Author:** ![deveshjawla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/deveshjawla/32/16048_2.png) [@deveshjawla](https://discourse.julialang.org/u/deveshjawla)\
**Post date:** [April 14, 2021, 6:58pm UTC](https://discourse.julialang.org/t/struct-with-new-instances-according-to-arguments-passed/59292/5 "2021-04-14T18:58:43Z")

</div>

Thanks for the explanation))

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [April 14, 2021, 9:46pm UTC](https://discourse.julialang.org/t/struct-with-new-instances-according-to-arguments-passed/59292/6 "2021-04-14T21:46:31Z")

</div>

You can also use `Base.@kwdef` which basically expands to @aramirezreyes’s answer:

```julia
using Base: @kwdef

@kwdef struct VariableStruct
    a = nothing
    b = nothing
    c = nothing
    d = nothing
    e = nothing
    f = nothing
end

VariableStruct(a=1) # ...

```
