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

Update Data of a DataState #74

Closed BeAddicted closed 7 months ago

BeAddicted commented 7 months ago

How can I update the data value of a DataState?

I have a DataState that should transition into itself and should update its data in this case. How can I do this?

I tried to make a dataTransition for the State into itself. But somehow I am not allowed to do that. java.lang.IllegalArgumentException: data transition should no be self targeted, use simple transition instead Similar error if I try to make a dataTransition without a targetState.

I dont really understand this limitation. Why is this allowed for normal transitions but not for DataTransitions? Is there another way to update the data value of a DataState?

nsk90 commented 7 months ago

Hi, looks I did not thought about such usecase, I will check this.

nsk90 commented 7 months ago

The reason of current behaviour was type safety guarantees.

For instance such self-targeted transitions should not be possible:

dataState<Int>(...) {
    dataTransition<Event, Float>(...) // note different type here
}
// or
state<Int>(...) { // not data state
    dataTransition<Event, Int>(...)
}

It is not quite easy to implement, as types are generic and are erased in runtime. I am checking what I can do to make this work.

nsk90 commented 7 months ago

https://github.com/nsk90/kstatemachine/releases/tag/v0.23.0 Fixed in new release

BeAddicted commented 7 months ago

Seems to work thank you