What is the deal with @kwdef

It is not in the manual, yet it seems to be ubiquitous in packages. What’s up with that?

Edit: online search finally yielded something useful: What does @kwdef do? - #4 by j-fu
I couldn’t find it before in this forum, because I was looking for @kwdef. No: the search fails with that. What it needs is kwdef. Go figure…

It does have the virtue of having actual source code documentation, which I used last time I forgot how to use it:

help?> @kwdef
  @kwdef typedef

  This is a helper macro that automatically defines a keyword-based
  constructor for the type declared in the expression typedef, which must be a
  struct or mutable struct expression. The default argument is supplied by
  declaring fields of the form field::T = default or field = default. If no
  default is provided then the keyword argument becomes a required keyword
  argument in the resulting type constructor.

  Inner constructors can still be defined, but at least one should accept
  arguments in the same form as the default inner constructor (i.e. one
  positional argument per field) in order to function correctly with the
  keyword outer constructor.

  │ Julia 1.1
  │
  │  Base.@kwdef for parametric structs, and structs with supertypes
  │  requires at least Julia 1.1.

  │ Julia 1.9
  │
  │  This macro is exported as of Julia 1.9.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> @kwdef struct Foo
             a::Int = 1         # specified default
             b::String          # required keyword
         end
  Foo

  julia> Foo(b="hi")
  Foo(1, "hi")

  julia> Foo()
  ERROR: UndefKeywordError: keyword argument `b` not assigned
  Stacktrace:
  [...]

julia>
2 Likes

It is in the manual – see also add at-kwdef to the manual · Issue #32659 · JuliaLang/julia · GitHub.

2 Likes