FSMs and GenFSM

285 views
Skip to first unread message

Robert Virding

unread,
Jan 16, 2015, 6:40:02 PM1/16/15
to elixir-l...@googlegroups.com
I was reading in a blog that from 0.13 the GenFSM had been removed. I hadn't noticed this before but on checking I see that there is no GenFSM in the documentation.

This really surprised me as I have always considered FSMs to be fundamental building blocks and extremely useful. There are so many problems which map naturally into FSMs.

Is there a natural replacement which just has a different name? Is there a link to the discussions around this decision? What is suggested instead when you need an FSM?

Robert

Paul Schoenfelder

unread,
Jan 16, 2015, 6:47:39 PM1/16/15
to elixir-l...@googlegroups.com

Hey Robert,

If I recall, the recommendation is currently to just use gen_fsm directly. I know someone also mentioned a project by Ulf Wiger the name of which I can't recall off the top of my head. I think the reasoning was that GenFSM was simply a wrapper, which is generally avoided in the standard library.

Paul

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Simpson

unread,
Jan 16, 2015, 7:02:22 PM1/16/15
to elixir-l...@googlegroups.com

Robert Virding

unread,
Jan 17, 2015, 11:42:46 AM1/17/15
to elixir-l...@googlegroups.com
Ulf Wiger's support tool is a parse transform which allows you to write FSMs in "classic" style and then adds clauses to receives which handle all extra messages needed to make it a behaviour compliant process. It's a cool idea.

No, I was wondering why just GenFSM is missing. Yes, it it a wrapper around the erlang gen_fsm module, but so are GenServer, GenEvent, Supervisor and Application. They are just wrappers around gen_server, gen_event, supervisor and application. So why GenFSM? Especailly as I think that FSMs can be very good at describing things and being able to code it in a style which clearly brings this out is a Big Win.

Robert

José Valim

unread,
Jan 17, 2015, 11:59:15 AM1/17/15
to elixir-l...@googlegroups.com
No, I was wondering why just GenFSM is missing. Yes, it it a wrapper around the erlang gen_fsm module, but so are GenServer, GenEvent, Supervisor and Application. They are just wrappers around gen_server, gen_event, supervisor and application. So why GenFSM? Especailly as I think that FSMs can be very good at describing things and being able to code it in a style which clearly brings this out is a Big Win.

Oh, nice discussion. Some rationale:

1. GenEvent and GenServer actually provide functionality on top of Erlang one. GenEvent is a complete rewrite (it adds streaming and monitored handlers while keeping the underlying message protocol) while GenServer provides some fixes regarding call and cast (which the OTP team knows about but cannot fix due to backwards compat)

2. Although we don't have anything planned for Application, it is an essential part of how OTP works (no application == no supervision tree), so it made sense to include a module with its functionality and documentation

3. We actually have improvements scheduled to GenServer and Supervisor in upcoming releases, which reinforces their inclusion in the stdlib

The issue is that: we don't have any of those for FSM. I do consider FSM to be more important than a GenEvent (at least at this moment) but if we aren't or won't add anything to it, there is no justification to not use the Erlang one.

The second point is that I am not even sure GenFSM is the best design API wise. When I wrote a FSM, I wrote using gen_fsm and then rewrote it with plain_fsm and I found the result better with the latter. But that was once and as such it does not provide enough "experience" to choose one over the other. My hope is that the community will standardize or come up with one which we can then include as part of stdlib.

Robert Virding

unread,
Jan 20, 2015, 12:11:13 PM1/20/15
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
I just sort of feel not having an elixir module for it tends to demote it. And people seem hesitant to use FSMs anyway so it needs all the support it can get.

Re the API, I don't know. You normally end up with one function per state, or at least you should, so the difference is very much the same as whether to use GenServer or roll-your-own server. Do the receive yourself or have it as a callback, you have the same issues as with GenServers. At least you can stop worrying if you got everything tail-called as it should. :-)

Robert
Reply all
Reply to author
Forward
0 new messages