# Can a struct be created with field keywords?

**URL:** <https://discourse.julialang.org/t/can-a-struct-be-created-with-field-keywords/27531>\
**Category:** New to Julia\
**Created:** [August 14, 2019, 1:28pm UTC](https://discourse.julialang.org/t/can-a-struct-be-created-with-field-keywords/27531 "2019-08-14T13:28:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [August 14, 2019, 1:28pm UTC](https://discourse.julialang.org/t/can-a-struct-be-created-with-field-keywords/27531/1 "2019-08-14T13:28:25Z")

</div>

I can do this:

```julia
julia> struct Mystruct
       x
       y
       end

julia> function (m::Mystruct)(z)
       m.x+m.y+z
       end

julia> m=Mystruct(1,2)
Mystruct(1, 2)

julia> m(3)
6

```

but I would like to do this:

```julia
m = Mystruct(x=1,
             y=2)

```

as my real struct has enough fields that using keywords would be much more readable, but it doesn’t work (unsupported keyword arguments).

I can get the behavior I want by writing an outer constructor:

```julia
julia> Mystruct(;x,y) = Mystruct(x,y)
Mystruct

```

but that seems a bit unwieldy.

At this point in writing this post, I’ve discovered Parameters.jl, which seems to do what I want but mentions that the functionality may come into the language itself soon.

Thoughts on which path I should take?

---

<div class="post-metadata">

**Author:** ![pmarg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmarg/32/936_2.png) [@pmarg](https://discourse.julialang.org/u/pmarg)\
**Post date:** [August 14, 2019, 3:43pm UTC](https://discourse.julialang.org/t/can-a-struct-be-created-with-field-keywords/27531/2 "2019-08-14T15:43:31Z")

</div>

The functionality is already in Base. You can try:

```julia
import Base.@kwdef

@kwdef struct Mystruct
    x = 1
    y = 2
end

```

It works like a normal `struct` and you can declare types as usual

```julia
@kwdef struct Mystruct{I <: Int64}
    x::I = 1
    y::I = 1
end

```

```julia
m = Mystruct()
Mystruct{Int64}(1, 1)

m = Mystruct(x=3)
Mystruct{Int64}(3, 1)

```

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [August 14, 2019, 4:25pm UTC](https://discourse.julialang.org/t/can-a-struct-be-created-with-field-keywords/27531/3 "2019-08-14T16:25:45Z")

</div>

Thank you! Knowing what to search for I can see that in the release notes here [https://pkg.julialang.org/docs/julia/THl1k/1.1.1/NEWS.html#Standard-library-changes-1](https://pkg.julialang.org/docs/julia/THl1k/1.1.1/NEWS.html#Standard-library-changes-1).
