Hellow! I have a code :
module Test
export custom_constructor_1, custom_constructor_2
struct Foo
a::Int
b::Int
c::Int
function Foo(a::Int, b::Int)
# @warn "You should use a custom constructor!" # this must work only in the default constructor
return new(a, b, a + b)
end
end
function custom_constructor_1(;
a::Int = 5,
b::Int = 8,
)
@info "Good, you use custom constructor № 1"
println("a=$a, b=$b")
return Foo(a, b)
end
function custom_constructor_2(;
a::Int = 3,
b::Int = 4,
)
@info "Good, you use custom constructor № 2"
println("a=$a, b=$b")
return Foo(a, b)
end
end # module Test
How to raise a warning when using the default constructor directly? like this:
julia> using .Test
julia> foo = Test.Foo(1,2)
┌ Warning: You should use a custom constructor!
└ @ Main.Test REPL[2]:10
Main.Test.Foo(1, 2, 3)
julia> foo = custom_constructor_1() # this is OK
[ Info: Good, you use custom constructor № 1
a=5, b=8
Main.Test.Foo(5, 8, 13)
julia> foo = Test.custom_constructor_2(a=3, b=4) # and this is OK
[ Info: Good, you use custom constructor № 2
a=3, b=4
Main.Test.Foo(3, 4, 7)