korken89 / smlang-rs

A State Machine Language DSL procedual macro for Rust
Apache License 2.0
192 stars 28 forks source link

Why can't starting state contain data? #34

Closed pleepleus closed 2 years ago

pleepleus commented 2 years ago

In the code, it says "because there is no way to propagate it (yet)". I don't fully understand this. Could someone more familiar with the reason explain?

pleepleus commented 2 years ago

After looking more at the code, I think this is referring to the signature for ::new(), which does not take any arguments so thus there is no way to pass initial data for the state. I think a simple non-breaking change would be to change this signature to accept initial state data if the initial state holds data.

ryan-summers commented 2 years ago

It looks like codegen.rs actually generates a function signature new_with_state, which theoretically allow the starting state to contain data. Have you tried this?

dzimmanck commented 2 years ago

@ryan-summers, yes that works, and does provide a work-around if you just create a dummy start state with no data. But that's ugly. We can do better with the macro. We simply detect if the starting state has data, and we tailor the input signature of sm::new so that if the starting state has data, it requires the initial data to be provided in the function.

dzimmanck commented 2 years ago

I am just flagging this as an enhancement and assigning to myself. It will be easy to implement. I plan on working on this Friday.

pleepleus commented 2 years ago

Closed by PR #39