# How to define a \`struct\` that has keyword arguments?

**URL:** <https://discourse.julialang.org/t/how-to-define-a-struct-that-has-keyword-arguments/114353>\
**Category:** General Usage\
**Tags:** question, type, struct\
**Created:** [May 16, 2024, 10:18am UTC](https://discourse.julialang.org/t/how-to-define-a-struct-that-has-keyword-arguments/114353 "2024-05-16T10:18:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 16, 2024, 10:18am UTC](https://discourse.julialang.org/t/how-to-define-a-struct-that-has-keyword-arguments/114353/1 "2024-05-16T10:18:59Z")

</div>

I want to define a `struct` whose instances are created with explicit keyword arguments just like a function. How?

```julia
struct MyStruct
    kwarg1::Real
    kwarg2::String
end

```

```julia
MyStruct(kwarg1=1, kwarg2="hello")

```

---

<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:** [May 16, 2024, 10:37am UTC](https://discourse.julialang.org/t/how-to-define-a-struct-that-has-keyword-arguments/114353/2 "2024-05-16T10:37:37Z")

</div>

See `@kwdef`

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 16, 2024, 10:42am UTC](https://discourse.julialang.org/t/how-to-define-a-struct-that-has-keyword-arguments/114353/3 "2024-05-16T10:42:19Z")

</div>

I think this might have been recommended to you before but for these very basic questions AI is pretty good:

[https://juliahub.com/ui/AskAI](https://juliahub.com/ui/AskAI)

> How can I define a struct that can be constructed using keywords?

> To define a struct in Julia that can be constructed using keywords, you can use the `Base.@kwdef` macro. This macro automatically defines a keyword-based constructor for the struct, allowing you to specify default values for fields and make certain keyword arguments required.  
> Here’s an example of how you can define a struct with keyword constructors using `Base.@kwdef`:

```julia
using Base

@kwdef struct MyType
    x1::Int = 0
    x2::Float64
end

```

> Now you can construct an instance of `MyType` using keyword arguments:

```julia
my_instance = MyType(x2=3.14)

```

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 16, 2024, 10:47am UTC](https://discourse.julialang.org/t/how-to-define-a-struct-that-has-keyword-arguments/114353/4 "2024-05-16T10:47:12Z")

</div>

After seeing the answer, I even suddenly felt as if I had asked the same question before, but couldn’t remember when. Why do I always forget some syntax? Does this happen to everyone?

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 16, 2024, 10:58am UTC](https://discourse.julialang.org/t/how-to-define-a-struct-that-has-keyword-arguments/114353/5 "2024-05-16T10:58:23Z")

</div>

> [@nilshg](#):
>
> [JuliaHub](https://juliahub.com/ui/AskAI)

I can’t login it:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/d/dd3ac8917886ba847e960d95428544749de8cbb5.png)

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 16, 2024, 10:59am UTC](https://discourse.julialang.org/t/how-to-define-a-struct-that-has-keyword-arguments/114353/6 "2024-05-16T10:59:43Z")

</div>

> [@nilshg](#):
>
> Now you can construct an instance of `MyType` using keyword arguments:

Can I use `MyType(0, 3.14)` at the same time?

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 16, 2024, 11:03am UTC](https://discourse.julialang.org/t/how-to-define-a-struct-that-has-keyword-arguments/114353/7 "2024-05-16T11:03:19Z")

</div>

> [@WuSiren](#):
>
> I can’t login it:

And Now I can’t even log out…

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 16, 2024, 11:04am UTC](https://discourse.julialang.org/t/how-to-define-a-struct-that-has-keyword-arguments/114353/8 "2024-05-16T11:04:37Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/4/c/4ceecc39a7e0aba16bdeb8841befecf8c6cbd01a.png)  
got stuck on this page…

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [May 16, 2024, 1:26pm UTC](https://discourse.julialang.org/t/how-to-define-a-struct-that-has-keyword-arguments/114353/9 "2024-05-16T13:26:28Z")

</div>

A `struct` does not have keywords, although it does have fields and those fields have names. A _constructor_ has keywords. The previously-discussed `@kwdef` simply creates a constructor with keywords to go along with the defined `struct` and its default constructor (which simply turns arguments into fields in the provided order). Using `@macroexpand` on the previous suggestions:

```julia-repl
julia> @macroexpand @kwdef struct MyType
           x1::Int = 0
           x2::Float64
       end
quote
    #= util.jl:609 =#
    begin
        $(Expr(:meta, :doc))
        struct MyType
            #= REPL[14]:2 =#
            x1::Int
            #= REPL[14]:3 =#
            x2::Float64
        end
    end
    #= util.jl:610 =#
    function MyType(; x1 = 0, x2)
        #= REPL[14]:1 =#
        MyType(x1, x2)
    end
end

```

Here we see that what `@kwdef` does is make the definition of the `struct` that we define, then it creates a constructor with keyword arguments corresponding to each field (along with default values, if specified) that calls the default constructor (with positional arguments) to make the object.

While the macro is a convenient and robust way to do this, you _could_ have simply defined that same constructor with keywords yourself. A constructor is just a function and has all the flexibility that a function offers.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 16, 2024, 2:31pm UTC](https://discourse.julialang.org/t/how-to-define-a-struct-that-has-keyword-arguments/114353/10 "2024-05-16T14:31:53Z")

</div>

> [@nilshg](#):
>
> I think this might have been recommended to you before but for these very basic questions AI is pretty good:

A simple google search is also pretty good. Googling “julia struct that can be constructed using keywords” turns up this as the first hit: [Can a struct be created with field keywords?](https://discourse.julialang.org/t/can-a-struct-be-created-with-field-keywords/27531)

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 16, 2024, 3:26pm UTC](https://discourse.julialang.org/t/how-to-define-a-struct-that-has-keyword-arguments/114353/11 "2024-05-16T15:26:59Z")

</div>

Thanks to everyone!
