Skip to content

class forward call definition problem #375

@paddy0489

Description

@paddy0489

berry.exe state.be is working well
but if i call berry and import state.be
than an redefinition error comes.

import "state.be" as state
syntax_error: state.be:17: redefinition of 'OffState'
stack traceback:
stdin:1: in function main

#- Step 1: Define the State Interface
class State()
    def handle(context):
        pass
    end
end
-#
# Step 2: Create Concrete States
var OffState = nil
class OnState
    def handle(context)
        print("Light is already ON. Switching it OFF now.")
        context.state = OffState()
    end
end

class OffState
    def handle(context)
        print("Light is OFF. Switching it ON now.")
        context.state = OnState()
    end
end


# Step 3: Create the Context
class LightSwitch
    var state
    def init()
        self.state = OffState()
    end
    def press()
        self.state.handle(self)
    end
end

# Testing the FSM
switch = LightSwitch()
switch.press()  # Expected: "Light is OFF. Switching it ON now."
switch.press()  # Expected: "Light is already ON. Switching it OFF now."
switch.press()  # Expected: "Light is OFF. Switching it ON now."

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions