[erlang-questions] What's going on here? (a function with bizarre behavior in Eshell.)

Hans Bolinder <>
Thu Apr 19 10:45:19 CEST 2007


[Julian Fondren:]
> $ erl
> Erlang (BEAM) emulator version 5.5.4 [source] [kernel-poll:false]
> 
> Eshell V5.5.4  (abort with ^G)
> 1> hello.
> hello
> 2> v(-1).
> hello
> 3> io:fwrite("~p", [v(-1)]).
> hellook
> 4> io:fwrite("~p", [v(-1)]).
> okok
> 5> Last = fun () -> io:fwrite("~p", [v(-1)]) end.
> #Fun<erl_eval.20.112921583>
> 6> Last().
> okok
> 7> f(Last), Last = fun () -> X = v(-1), io:fwrite("~p", [X]) end.
> #Fun<erl_eval.20.112921583>
> 8> Last().
> okok
> 9> 1.
> 1
> 10> Last().
> okok
> 11> f(Last), Last = fun () -> X = v(-1), io:fwrite("~p", [X]) end.
> #Fun<erl_eval.20.112921583>
> 12> f(Last), Last = fun () -> X = v(-1), io:fwrite("~p", [X]) end.
> #Fun<erl_eval.20.112921583>
> 13> Last().
> #Fun<erl_eval.20.112921583>ok
> 14> 1.
> 1
> 15> Last().
> #Fun<erl_eval.20.112921583>ok
...
> I still have no clue what actually implements v/1

Substituting values for uses of v/1, which is one of the tasks of
shell:expand_expr/3, is done immediately after the command string has
been read.

In command 12 the return value of command 11 is substituted for v(-1)
in the body of the fun; command 15 will not use the value of command
14 but the value of command 11.

The command b() displays the bodies of functions defined at the shell
prompt, like this:

6> b().
Last =
    fun() ->
           io:fwrite("~p", [ok])
    end
ok

Unfortunately
16> b()
seems to trigger a bug. It will, of course, be fixed.

Best regards,

Hans Bolinder, Erlang/OTP team



More information about the erlang-questions mailing list