https://kotlinlang.org logo
Title
s

simon.vergauwen

10/08/2019, 10:13 AM
@thanh what is the goal you’re trying to achieve?
t

thanh

10/08/2019, 10:21 AM
I want to work on some kind of type safe state machine.
s

simon.vergauwen

10/08/2019, 10:22 AM
Then I’d advice looking into
IndexedStateT
. I reported the issue and it should’ve been fixed in 1.3.4x but I haven’t had the time to update that branch yet.
Sadly there is not a lot of info on this 😞 but expanding on the small example from Cats you should be able to figure out the rest. If you need any help or are interested in updating that branch I’d love to help 🙂
t

thanh

10/08/2019, 10:24 AM
that's very kind of you. I'll check it as well as read the link you gave.
I'm interested on updating that branch
🎉 1
c

corneil

10/21/2019, 1:26 PM
You could take a look at a project I did to provide an FSM implementation with a DSL to simplify configuration: https://github.com/open-jumpco/kfsm/
t

thanh

10/21/2019, 1:59 PM
Nice library @corneil, but I need a more safe statemachine library (compiler level). Like if the door is closed, you cannot close it again.
c

corneil

10/21/2019, 2:00 PM
The statemachine would take care of all the state management. What is your use-case?
KFSM will throw exception when you send an event for which there is no matching transition.
So if you define:
state(OPENED) {
    whenEvent(CLOSE to CLOSED) {
    }
}
state(CLOSED) {
    whenEvent(OPEN to OPENED) {
    }
}
In this case a CLOSE event when state is CLOSED will cause an exception.
t

thanh

10/21/2019, 2:45 PM
I understood that, but I want to prevent sending CLOSE event when the door is closed. Which is described here: https://typelevel.org/cats/datatypes/state.html#changing-states
:arrow: 1
c

corneil

10/21/2019, 2:53 PM
I've always viewed a state machine like a box with buttons. Sometimes the buttons do something, other times they do nothing and other times they cause an alarm. With KFSM you can even ask the state machine which events are allowed in current state or for given state so that you can enable/disable buttons/controls.
I can see a need for State management like in Cats. KFSM was designed for the cases where a UI or service endpoint has to handle events. In the case of the service the FSM is initialised with previously persisted state and then event(s) are sent.
👍 1