www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - State Machine

reply Tim <t.oliver windowslive.com> writes:
Hi there,

I was wondering if there is a very D approach to making a state 
machine. I ask this because I could mangle something together but 
if there is a language feature that could make my life a lot 
easier or readable then that would be fantastic. The main thing 
I'm wondering about is being able to run some sort of arbitrary 
code in the form of strings (maybe using string mixins) as the 
machine exits a state
Jan 01
parent reply RSY <rsy_881 gmail.com> writes:
On Saturday, 2 January 2021 at 05:19:08 UTC, Tim wrote:
 Hi there,

 I was wondering if there is a very D approach to making a state 
 machine. I ask this because I could mangle something together 
 but if there is a language feature that could make my life a 
 lot easier or readable then that would be fantastic. The main 
 thing I'm wondering about is being able to run some sort of 
 arbitrary code in the form of strings (maybe using string 
 mixins) as the machine exits a state
I found this on github, that might be what you are looking for: https://github.com/mintyfresh/state-machine It make uses of mixins, wich looks very nice
Jan 02
next sibling parent Tim <t.oliver windowslive.com> writes:
On Saturday, 2 January 2021 at 08:29:44 UTC, RSY wrote:
 On Saturday, 2 January 2021 at 05:19:08 UTC, Tim wrote:
 Hi there,

 I was wondering if there is a very D approach to making a 
 state machine. I ask this because I could mangle something 
 together but if there is a language feature that could make my 
 life a lot easier or readable then that would be fantastic. 
 The main thing I'm wondering about is being able to run some 
 sort of arbitrary code in the form of strings (maybe using 
 string mixins) as the machine exits a state
I found this on github, that might be what you are looking for: https://github.com/mintyfresh/state-machine It make uses of mixins, wich looks very nice
This looks pretty good, thanks for that. It could get a little annoying having to create actual named functions for things that are only executed in one place. Any chance there may be a way to use the transition parameters with a code block rather than a full function?
Jan 02
prev sibling parent Tim <t.oliver windowslive.com> writes:
On Saturday, 2 January 2021 at 08:29:44 UTC, RSY wrote:
 On Saturday, 2 January 2021 at 05:19:08 UTC, Tim wrote:
 Hi there,

 I was wondering if there is a very D approach to making a 
 state machine. I ask this because I could mangle something 
 together but if there is a language feature that could make my 
 life a lot easier or readable then that would be fantastic. 
 The main thing I'm wondering about is being able to run some 
 sort of arbitrary code in the form of strings (maybe using 
 string mixins) as the machine exits a state
I found this on github, that might be what you are looking for: https://github.com/mintyfresh/state-machine It make uses of mixins, wich looks very nice
Let me explain a little better. I'd love to be able to do this sort of thing: BeforeTransition("ordered") { int foo = 0; ... }
Jan 02