# Automatic keyword argument constructor

**URL:** https://discourse.julialang.org/t/automatic-keyword-argument-constructor/36573
**Category:** General Usage
**Created:** [March 26, 2020, 10:58pm UTC](https://discourse.julialang.org/t/automatic-keyword-argument-constructor/36573 "2020-03-26T22:58:24Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![HugoTrentesaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugotrentesaux/32/12583_2.png) [@HugoTrentesaux](https://discourse.julialang.org/u/HugoTrentesaux)
#### Post date: [March 26, 2020, 10:58pm UTC](https://discourse.julialang.org/t/automatic-keyword-argument-constructor/36573/1 "2020-03-26T22:58:24Z")

</div>

Is there a way to have Julia automatically defining a constructor using keywords arguments?

```julia
# the composite type
struct Person
  name::String
  age::Int
end
# generates automatically this constructor:
me = Person("Hugo", 24)
# is there a way to define automatically
# this constructor using only keyword arguments
# corresponding to fields names?
me = Person(;name="Hugo", age=24)

```

I think this could be achieved using macros, but I’m not good at it and do not know how to do. Now, I’m forced to implement it manually:

```julia
struct Person
  name::String
  age::Int
  Person(;name::String,age::Int) = new(name,age)
end

```

It’s a bit cumbersome.

---

<div class="post-metadata">

### Author: ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)
#### Post date: [March 26, 2020, 11:00pm UTC](https://discourse.julialang.org/t/automatic-keyword-argument-constructor/36573/2 "2020-03-26T23:00:05Z")

</div>

I believe [Parameters.jl](https://github.com/mauro3/Parameters.jl) is exactly what you’re looking for

---

<div class="post-metadata">

### Author: ![HugoTrentesaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugotrentesaux/32/12583_2.png) [@HugoTrentesaux](https://discourse.julialang.org/u/HugoTrentesaux)
#### Post date: [March 26, 2020, 11:07pm UTC](https://discourse.julialang.org/t/automatic-keyword-argument-constructor/36573/3 "2020-03-26T23:07:21Z")

</div>

Thanks, it does the trick!

```julia
import Parameters.with_kw

@with_kw struct Person
   name::String
   age::Int
end

julia> Person(name="Hugo", age=24)
Person
  name: String "Hugo"
  age: Int64 24

```

_note_: it also changes the way the structure prints itself, without Parameters.jl, it was

```julia
julia> Person(name="Hugo", age=24)
Person("Hugo", 24)

```

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [March 27, 2020, 12:48am UTC](https://discourse.julialang.org/t/automatic-keyword-argument-constructor/36573/4 "2020-03-27T00:48:10Z")

</div>

There’s also `Base.@kwdef`. It’s very poorly advertised, though. It should probably be mentioned in the Constructors chapter in the manual.

---

<div class="post-metadata">

### Author: ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)
#### Post date: [July 9, 2021, 1:17pm UTC](https://discourse.julialang.org/t/automatic-keyword-argument-constructor/36573/5 "2021-07-09T13:17:22Z")

</div>

perhaps `Base.@kwdef` is not documented because it is not exported, is hence considered an internal function, and might be removed in future? seems very useful though. definitely a keeper!

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [July 9, 2021, 1:39pm UTC](https://discourse.julialang.org/t/automatic-keyword-argument-constructor/36573/6 "2021-07-09T13:39:46Z")

</div>

Parameters.jl also export `@unpack`, which is very useful.

---

<div class="post-metadata">

### Author: ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)
#### Post date: [July 21, 2023, 10:01am UTC](https://discourse.julialang.org/t/automatic-keyword-argument-constructor/36573/7 "2023-07-21T10:01:08Z")

</div>

> [@CameronBieganek](#):
>
> There’s also Base.@kwdef. It’s very poorly advertised, though. It should probably be mentioned in the Constructors chapter in the manual.

For the reference, since Julia 1.9 (release April 2023), the `@kwdef` macro is exported, see [Julia 1.9 release NEWS: Standard library changes](https://docs.julialang.org/en/v1.9/NEWS/#Standard-library-changes).

This was implemented in PR

> <https://github.com/JuliaLang/julia/pull/46273>
>
> Discussed and approved by triage https://github.com/JuliaLang/julia/issues/33192…#issuecomment-1191810619
> 
> Fixes https://github.com/JuliaLang/julia/issues/33192
> Fixes https://github.com/JuliaLang/julia/issues/32659
> 
> Perhaps this should also close https://github.com/JuliaLang/julia/issues/10146 ?

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 21, 2023, 10:10am UTC](https://discourse.julialang.org/t/automatic-keyword-argument-constructor/36573/8 "2023-07-21T10:10:50Z")

</div>

I am only adding this here for context (since the `@kwdef` only appears [documented](https://docs.julialang.org/en/v1/base/base/#Base.@kwdef) in `Base` - so people reading the manual might not easily encounter it).

Please note that by using `@kwdef`, you can also set some defaults for your fields:

```julia
@kwdef struct Person
  name::String="World"
  age::Int=42
end

```

By defining your `struct` as above, you are also provided with `Person()` constructor (obviously - there are not good defaults for a person, but this can come in handy when defining some configuration or you just have reasonable default values).

---

<div class="post-metadata">

### Author: ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)
#### Post date: [July 21, 2023, 1:49pm UTC](https://discourse.julialang.org/t/automatic-keyword-argument-constructor/36573/9 "2023-07-21T13:49:29Z")

</div>

> [@algunion](#):
>
> I am only adding this here for context (since the `@kwdef` only appears [documented](https://docs.julialang.org/en/v1/base/base/#Base.@kwdef) in `Base` - so people reading the manual might not easily encounter it).

Yes, I opened an issue on this topic ([add at-kwdef to the manual · Issue #32659 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/32659)) but didn’t went to the next level of submitting a PR to add `@kwdef` to the manual.

---

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [January 11, 2024, 1:57am UTC](https://discourse.julialang.org/t/automatic-keyword-argument-constructor/36573/10 "2024-01-11T01:57:06Z")

</div>

`my_func(; a, b, other... )`  
This syntax allows `my_func` to be called with additional arguments that are ignored.

I know you could write a similar constructor for a struct.  
It would be nicie if there was a flag that did it automatically. e.g. allow:

```julia
@kwdef @allow_other_args struct X  
    x::Int
    y::Int    
end

```

To be created with

```julia
X(x=1, y=2, w=10, z=100, a="something_else" )

```

Use case: I convert a database table to a vector of structs of different types (each row becomes a struct). I currently have to filter the fields of each row to match the struct.  
It would be nice to just use `AsTable(:)` to feed each row in as a named tuple and have the requried fields selected automatically.
