Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

UnrealScript has (had?) built-in syntactic support for state machines.

I think Jonathan Blow had some ideas for language support for ECS in Jai, or at least support for structure-of-array stuff. I'm not sure if it panned out.

My experience with both of those patterns is that each implementation tends to have enough unique requirements that it's hard for a language to find a sweet spot where built-in syntax can cover enough real-world use cases.

For example, off the top of my head, with state machines I've had questions like:

* Can states be defined dynamically or are they all known at compile time?

* Likewise, is the set of possible transitions fixed (and declarative) or freely specified at runtime?

* Does every instance of a given state machine always have the same set of states or do some vary?

* Can you attach arbitrary data to states? Arbitrary behavior?

* Can you attach arbitrary data to state transitions? Arbitrary behavior?

* Is the predicate logic for determining when to perform a state transition declarative and built-in, or can arbitrary imperative code cause a state transition?

* Are transitions synchronous? Asynchronous? Either?

* Can outside code observe when transitions occur in some way?

* Is the current state publicly accessible or encapsulated?

* Are states first-class objects? Are transitions?

I've tried just to build a reusable FSM library and even that wasn't very successful because there's just so much variance in each implementation.



> Can states be defined dynamically or are they all known at compile time?

You can say that about classes as well, give different answer and have languages that support one of those or both.

Similar questions exist about a lot of concepts that landed in some or many languages, like objects, async, events, promises, continuations. In most of them we achieved some sort of fuzzy consensus of what their capabilities should be.


I actually don't think we have achieved any kind of consensus here which is why there are a proliferation of popular languages.

Concurrency is handled in wildly different ways between C/C++/Java/C# (threads), Go/Lua (goroutines/coroutines), JavaScript/Dart (async promises), etc.

Object-oriented programming is interpreted so differently by different languages that people can't even agree on what the term means. Do you have multiple inheritance or not? Are methods always virtual, virtual by default, or non-virtual by default? Is there overloading? What control do you have over inheritance? Are there interfaces or not? How are static methods handled?


We needed a state machine to manage app startup in Ardour, and initially (since we're using C++) we used a boost library for this.

It was pretty cool - you just create a text based table describing state transitions and various methods that execute before, after and as a result of the transition, and it generates all the code for you. It was very, very quick to get things basically working with this template library.

But then ... we had to start dealing with corner cases and some fiddly little details of precisely how this FSM was supposed to work.

We still have the table in a comment in the code, because it's probably one of the easiest things to read to understand the FSM, but the implementation is now just the usual C/C++ nested switch statements.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: