5 ms·
I feel I've read a number of pieces on programming that talk about the evils of if statements (on which I more or less agree) and advocating their replacement b
by jgwil2 7y ago
I feel I've read a number of pieces on programming that talk about the evils of if statements (on which I more or less agree) and advocating their replacement by state machines. However I've never seen a super simple implementation of this that seems to obviously fulfill most branching use cases. The closest is the state/strategy pattern, which is great but if patterns (as it's sometimes said) reflect deficiencies in a language, what would a language with first-class state machines look like?
- janci 7y agoVHDL is close. If you define nextstate logic (logic expression that determines next state based on current state and current input) and nextstate latch (that transfers calculated next state to current state register) it gets recognized as finite state machine and optimized by the synthetizer.
- monocasa 7y agoState machines are pretty simple in rust. Pseudo code to get the gist: state = match (state, event) { (State1, Event1) => { State2 } (State1, Event2) => { ... } ... };
- jgwil2 7y agoDoes this end up replacing a large portion of if/else or switch statements?
- heavenlyblue 7y agoThis is basically a switch statement :)
- jgwil2 7y agoIf it evaluates top to bottom in an order-dependent fashion then I suppose so. If it is order independent then it's more like what I want from a state machine.
- monocasa 7y agoWhat exactly are you looking for wrt to "order independent". Like, this will compile down to a computed jump table if that's what you're getting at.
- jgwil2 7y agoInsert new cases or change order without affecting behavior and statically check that all cases are covered. Unlike a switch statement or if else block where each condition is checked in order so adding more cases gets difficult.
- monocasa 7y agoAll Rust match statements as statically checked that all cases are covered by the compiler; you can't really turn that of even with unsafe blocks. How are you expecting that inserting or adding statements would change behavior?
- FpUser 7y agoOn a level you've just shown they're pretty simple in nearly any language. I do use state machines quite often in my code but mostly on a different level. Multiple threads communicating to each other via home grown PS bus
- monocasa 7y agoThe ADTs of state and event let you encode the validity of access to data, and whether you've fully enumerated your state/event tuples in a way that isn't available in a lot of languages typically used when writing state machines.
- FpUser 7y agoSounds like you're trying to "scare me" into language. I have what I believe is superior implementation and have no problem counting states. Thank you but I am fine. It is not that I am against the Rust. Rich ecosystem is always good. Personally however I feel no real need for Rust at all. I understand that it is trying to be better C++ but frankly with modern C++ its libraries and tooling I've got enough safety features up to my gills. I do not remember when was the last time my products had memory leak, overrun etc. I do use bunch of other languages but so far had zero reason to use Rust. I live off my own products or products I create for other companies on contract basis and from that perspective adding yet another language would not earn me one extra penny. Rather quite the opposite.
- monocasa 7y agoThat's a really defensive response. I'm not telling you to do anything or trying to "scare you". I was literally just saying how it has been helpful to someone who previously was a lead on a C++ RTOS and wrote a lot of state machines, both in C++ and in Rust.
- FpUser 7y agoThat's a really defensive response. I'm not telling you to do anything or trying to "scare you" It sure sounded this way (trying to scare) to me but if I am wrong please accept my apology. Besides I did say "sounds like" ;)
- jgodbout 7y agoUsing generic methods in lisp worth eql keyword for state change is a natural way of doing this.
- joshuak 7y agoTake a look at the Ragel State Machine Compiler. http://www.colm.net/open-source/ragel/ http://www.colm.net/open-source/ragel/ http://www.colm.net/files/ragel/ragel-guide-6.10.pdf http://www.colm.net/files/ragel/ragel-guide-6.10.pdf