Open x37v opened 2 years ago
I could also see allowing for the match guard style syntax:
*State1 + Button(ButtonData { index, down : true } if index < 20) = State2
BTW.. i ended up forking and implementing an approach that allows for patterns in events and uses match guards to implement the guards.. its a bit more verbose and less "tied together" (you have to implement the Events
enum yourself).. but has some other benefits..
https://github.com/x37v/smlang-rs
I figure its a relatively big departure so I'm not sure if a PR to get it back here would make sense.
here is an example of the DSL:
statemachine! {
transitions: {
*State1 + ButtonEvent(Button { down: true, .. }) / ctx.action(event); = State2,
State1 + ButtonEvent(_) [!event.down] / {ctx.action(event)} = State3(2),
State1 + FooEvent("blah") = State3(30),
State3(usize) + ButtonEvent(Button { down: true, ..}) [event.index == 20 && *state < 20]
/ { ctx.action(event); println!("foo {}", state) } = State3(ctx.action2(*state, event)),
State3(usize) + ButtonEvent(Button { down: false, ..}) = State3(*state + 1),
//can't express State3(0) + FooEvent = State1 but can use a guard
State3(usize) + FooEvent("blah") [state == &0] = State1,
State5(StateData) + FooEvent("blah") [state.num == 0] = State1,
State5(StateData) + BarEvent(0) = State1,
}
}
Following on to my question in #23, I'm realizing that guards won't solve my problems, specifically, I'm implementing a series of menus that are driven by a pad of 16 buttons.. I'm trying to avoid creating unique events per button press/release while also allowing for using constants to identify details of the event matching so that I might easily use that same data to identify where to display something and potentially change the value of the constant (button that represents the transition/display area) in the future.
I'm wondering if it would make sense to expand the Event syntax to allow for pattern matching, so you can reuse the same event but with different patterns to specify different transitions:
which I assume would generate code somewhat like: