I want to use the @assert macro with a struct. The @with_kw macro from package Parameters can be used with no explicit method built on top of the struct, whereas Base.@kwdef needs an explicit method. Did I understand this correctly?
# 1. outer constructor with "semi-colon" keyword + no keyword macro + @assert
struct myStruct1
a :: Float64
end
function myStruct1(;a::Float64=1.0)
@assert a > 0 "must be positive"
myStruct1(a)
end
myStruct1().a
## 1.0
myStruct1(a = -1.0).a
## ERROR: LoadError: AssertionError: must be positive
# 2.1. Base.@kwdef + no outer constructor + @assert
Base.@kwdef struct myStruct2
a :: Float64 = 1.0
@assert a > 0 "must be positive" # was hoping this would work
end
myStruct2().a
## ERROR: LoadError: UndefVarError: a not defined
# 2.2. Base.@kwdef + outer constructor + @assert
Base.@kwdef struct myStruct22
a :: Float64 = 1.0
end
function myStruct22(;a::Float64=1.0) # explicit method just to @assert
@assert a > 0 "must be positive"
myStruct22(a)
end
myStruct22().a
## 1.0
myStruct22(a = -1.0).a
## ERROR: LoadError: AssertionError: must be positive
# 2.3. @with_kw + outer constructor + @assert
using Parameters
@with_kw struct myStruct23
a :: Float64 = 1.0
@assert a > 0 "must be positive" # works without explicit method
end
myStruct23().a
## 1.0
myStruct23(a = -1.0).a
# ERROR: LoadError: AssertionError: must be positive