plain_fsm icon indicating copy to clipboard operation
plain_fsm copied to clipboard

Avoid warnings in generated code for underscored variable names

Open tomas-abrahamsson opened this issue 3 years ago • 1 comments

In Erlang 24, it is a warning to match a variable that begins with an underscore. Avoid this for generated code with variables such as __FSM_State.

With the following program,

-module(x).
-compile({parse_transform,plain_fsm_xform}).
-export([data_vsn/0, x/1]).

data_vsn() ->
    1.

x(State) ->
    plain_fsm:extended_receive(
      receive
          _X ->
              x(State)
      end).

I got the warning below. The problem with the warning is that it can be an error if one has warnings_as_errors.

1> c("x", [debug_info]).
x.erl:0: Warning: variable '__FSM_Parent' is already bound. If you mean to ignore this value, use '_' or a different underscore-prefixed name
{ok,x}

The interesting idea to use a $ in the generated variable name to avoid the warning was not mine, but thanks to @attah

tomas-abrahamsson avatar May 28 '21 18:05 tomas-abrahamsson

In retrospect, the approach to using $ in the generated variable names was maybe not that good. I realized the generated code will for example not survive a round-trip via text form if parsed back. Maybe such a use-case is not so common, but debugging the plain_fsm itself could become tedious. I'll try to rework this PR to use something else to make the variables unlikely to collide yet still following variable name syntax.

tomas-abrahamsson avatar Jun 02 '21 11:06 tomas-abrahamsson

Addressed in #22. Thanks, and sorry for the delay.

uwiger avatar Oct 31 '22 18:10 uwiger