[erlang-questions] bad_return_from_state_function on "3.21 Complex State” of "OTP design principles"

by by@REDACTED
Wed Dec 13 09:01:56 CET 2017


Hi,

I am trying the example on http://erlang.org/doc/design_principles/statem.html#id77558 <http://erlang.org/doc/design_principles/statem.html#id77558>
"3.21 Complex State”

But I got exception below:
====================================
Erlang/OTP 20 [erts-9.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V9.0  (abort with ^G)
1> c(code_lock3).
{ok,code_lock3}
2> code_lock3:start_link([1,2,3], 4).
Locked
{ok,<0.67.0>}
3> code_lock3:button(1).
Locked
** exception exit: {bad_return_from_state_function,
                       {keep_state,
                           #{code => [1,2,3],remaining => [2,3]},
                           30000,
                           [{reply,
                                {<0.60.0>,#Ref<0.235851222.49545217.215838>},
                                ok}]}}
     in function  gen_statem:parse_event_result/8 (gen_statem.erl, line 1318)
     in call from gen_statem:loop_event/6 (gen_statem.erl, line 1015)
     in call from proc_lib:init_p_do_apply/3 (proc_lib.erl, line 247)
4> 
=ERROR REPORT==== 13-Dec-2017::15:46:09 ===
** State machine code_lock3 terminating
** Last event = {{call,{<0.60.0>,#Ref<0.235851222.49545217.215838>}},
                 {button,1}}
** When server state  = {{locked,4},#{}}
** Reason for termination = error:{bad_return_from_state_function,
                                   {keep_state,
                                    #{code => [1,2,3],remaining => [2,3]},
                                    30000,
                                    [{reply,
                                      {<0.60.0>,
                                       #Ref<0.235851222.49545217.215838>},
                                      ok}]}}
** Callback mode = [handle_event_function,state_enter]
** Stacktrace =
**  [{gen_statem,parse_event_result,8,[{file,"gen_statem.erl"},{line,1318}]},
     {gen_statem,loop_event,6,[{file,"gen_statem.erl"},{line,1015}]},
     {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,247}]}]
====================================

I think the problem is on this:
		[Digit|Rest] -> % Incomplete
		    {keep_state, Data#{remaining := Rest, 30000},
		     [{reply,From,ok}]};
Change code to below got the **exception
		[Digit|Rest] -> % Incomplete
		    {keep_state, Data#{remaining := Rest}, 30000,
		     [{reply,From,ok}]};

Actually, this is apparently wrong, since the map can not got “30000" in this case. I tried move “30000" out of the map, then the above exception occurs.
And, I also tried just remove the “30000”, and it works fine.
		[Digit|Rest] -> % Incomplete
		    {keep_state, Data#{remaining := Rest},
		     [{reply,From,ok}]};

My question is:
Is this a typo in the documentation?
Or
Am I missing something?

Thanks
by
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20171213/fd61204f/attachment.htm>


More information about the erlang-questions mailing list