KStateMachine / kstatemachine

KStateMachine is a Kotlin DSL library for creating state machines and statecharts.
https://kstatemachine.github.io/kstatemachine/
Boost Software License 1.0
339 stars 19 forks source link

Need explanation for choice states #98

Closed piefel closed 2 months ago

piefel commented 2 months ago

The choice states do not appear in any example. The unit test is very minimal, and it does not include actual choices. In docs/index.md, the example code includes event.value, which is not even valid.

It seems a choiceDataState should have access to the data, but it appears that is not the case. Or perhaps it has access, but again there is no mention of it and the unit tests also do not use it.

nsk90 commented 2 months ago

Hi, I will add more descriptive sample in docs. Your assumption is almost correct.

event.value just means that you have some event like this class MyEvent(val value: Int): Event

choiceDataState is used in this unit test. https://github.com/KStateMachine/kstatemachine/blob/master/tests/src/commonTest/kotlin/ru/nsk/kstatemachine/state/ChoiceStateTest.kt#L130

choiceDataState is actually necessary not for accessing data field, but to be used as target of dataTransition function (for type safety). It chooses between DataStates of same type.

choiceDataStates lambda does not provide guarantee that only DataEvent can trigger it. It potentially can be called with any type of Event, so explicit cast in users (your) code is necessary to access data field of DataEvent.

nsk90 commented 2 months ago

You can find more descriptive samples here https://kstatemachine.github.io/kstatemachine/#choice-state

piefel commented 1 month ago

Thank you, that was really quick and very helpful.