Open Shuvomoy opened 3 weeks ago
@mk
is a shorthanded representation for something like a function call, where an LHS like a
in a = 0
works like a keyword argument.
You may know the following 2 examples are not equivalent:
# case 1
a_input < 0 ? func(a = 1) : func(a = 100)
# case 2
func(a_input < 0 ? (a = 1) : (a=100))
The solution is to lift your if-else
up:
@oodef mutable struct MyType8
a # I recommend you to use Generics or concrete types here
b
function new(; a_input = 30, b_input = 50)
if a_input < 0
a = 0
elseif a_input > 100
a = 100
else
@info "a_input is in the correct range"
a = a_input
end
b = b_input;
@mk begin
a = a
b = b
end
end
end
Writing the logics this way also helps me find out a potential bug (missing a
when a_input
in the correct range) in your example.
It makes sense, closing the issue. It may help to include similar examples in the documentation in the future. Thanks very much again!
I am trying to implement the following Python class into equivalent Julia code using
ObjectOriented.jl
It seems that
ObjectOriented.jl
does not accept if-else statement within an@mk
block. For example the following code returns the errorLoadError: invalid construction statement if a_input < 0
: