Skip to content

[Documentation Troubleshooting]: Implementing if-else block in @mk gives invalid construction statement error #15

@Shuvomoy

Description

@Shuvomoy

I am trying to implement the following Python class into equivalent Julia code using ObjectOriented.jl

## Python code
class MyType8:
    def __init__(self, a_input=30, b_input=50):
        # Clamping a_input within the range 0 to 100
        if a_input < 0:
            self.a = 0
        elif a_input > 100:
            self.a = 100
        else:
            self.a = a_input
            print("a_input is in the correct range")
        
        self.b = b_input

def myfun_type8(self):
    return self.a ** 2

def myfun2_type8(self, other):
    return self.a ** 2 + self.b ** 2 + other ** 2

# Creating instances
test1 = MyType8()

test2 = MyType8(a_input=200, b_input=50)

# Using the functions
result1 = myfun_type8(test2)
print("Result of myfun_type8(test2):", result1)

result2 = myfun2_type8(test2, 100)
print("Result of myfun2_type8(test2, 100):", result2)

It seems that ObjectOriented.jl does not accept if-else statement within an @mk block. For example the following code returns the error LoadError: invalid construction statement if a_input < 0:

## Julia code 
using ObjectOriented
@oodef mutable struct MyType8

    a
    b

    function new(; a_input = 30, b_input = 50)

        
        @mk begin
            if a_input < 0
                a = 0
            elseif a_input > 100
                a = 100
            else
                @info "a_input is in the correct range"
            end
            b = b_input
        end
     
    end

end

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions