[erlang-questions] Help - Can I stop this warning?

Richard Carlsson <>
Sat Sep 22 22:12:13 CEST 2007


G Bulmer wrote:
> So, this is what I would expect to be correct:
> -define(FUN, ((fun(X) -> {_,{_,F,_}} = X, F end)
>                                        (process_info(self 
> (),current_function))) ).
> -define(IOFUN(), io:format("~p: ", [?FUN])).
> 
> test0() ->
>      ?IOFUN(), io:format("explode if F is bound~n"),
>      F = wibble,
>      io:format("clearly, everything is fine with F, and wibble ==  
> ~p~n", [F]),
>      ?IOFUN(), io:format("Finished.~n").
> 
> Unfortunately, this fails:
> 11> scriptbug:test0().
> test0: explode if F is bound
> clearly, everything is fine with F, and wibble == wibble
> 
> =ERROR REPORT==== 22-Sep-2007::18:35:21 ===
> Error in process <0.69.0> on node '' with exit  
> value: {{badmatch,{current_function,{scriptbug,test0,0}}}, 
> [{scriptbug,'-test0/0-fun-1-',1},{erl_eval,do_apply,5},{shell,exprs, 
> 6},{shell,eval_loop,3}]}

It looks like the problem is the _second_ invocation of ?IOFUN.
At that point, F is bound (by F = wibble), so it becomes imported
into the {_,{_,F,_}} pattern in the body of the fun.

It is only variables in the parameters (the parameter patterns) of the
fun that will shadow surrounding bindings. Since F only occurs in the
body, it becomes imported if it is bound in the surrounding environment,
even if it occurs in a pattern match.

What you want is:
  -define(FUN, ((fun({_,{_,F,_}}) -> F end)(...))).

    /Richard




More information about the erlang-questions mailing list