I stumbled upon it in Plots.jl docs. But can’t find a description anywhere.
I searched the official docs, and its pdf version. Looked at the Manual tab.
On google and discourse alike, I’m able to find posts like this one, where there’s discussion of the future of Base.@kwdef, but here I’m unsure of what it even does.
Could someone please point me in a direction?
Thank You
Cheers
help?> Base.@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.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> Base.@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:
[...]
Since it is not exported nor in the manual (as reflects its origins as an internal utility), I think* this means it is not part of Julia’s public API and could be changed in a minor version, although it’s used extensively in public packages and private code, so I hope it wouldn’t be broken too much out of consideration for that. (And public package breakage is definitely taken into account when releasing new Julia versions, via PkgEval).