Kwonly.jl provides a macro @add_kwonly
. It creates a keyword-only
version of the given function. Example:
using Kwonly
struct A
x
y
@add_kwonly A(x, y=2) = new(x, y)
end
This macro add a keyword-only constructor by expanding A(x, y=2) = new(x, y)
into:
A(x, y) = new(x, y) # original
A(; x = throw(UndefKeywordError(:x)), y=2) = new(x, y) # keyword-only
So, the struct A
can also be constructed by using only keyword
arguments:
@test A(1) == A(x=1)