So this internal :new
expression seems to be working again. I’m wondering if it’s possible to unsafely skip inner constructors’ validation to instantiate tweaks to existing instances with setting syntax like Accessors.jl.
julia> macro new(call::Expr)
if call.head != :call
return call
else
Expr(:new, call.args...)
end
end
@new (macro with 1 method)
julia> struct EvenInt
x::Int
function EvenInt(x::Int)
iseven(x) || throw(ArgumentError("Must be even."))
sleep(1) # pretend this is useful processing
new(x)
end
end
julia> @time y = EvenInt(2)
1.002108 seconds (4 allocations: 112 bytes)
EvenInt(2)
julia> @time @new(EvenInt(1)) # skips validation like mutation
0.000001 seconds
EvenInt(1)
julia> using Accessors
julia> @time @reset y.x = 4 # goes through inner constructor
1.034079 seconds (5.05 k allocations: 260.352 KiB, 2.56% compilation time: 100% of which was recompilation)
EvenInt(4)