Is there a way to have Julia automatically defining a constructor using keywords arguments?
# 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:
struct Person
name::String
age::Int
Person(;name::String,age::Int) = new(name,age)
end
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!