Using bound variables in a fun's function clause

Vincent de Phily <>
Mon Oct 18 15:29:34 CEST 2010


Hi all,

consider the following snipnet :

> #!/usr/bin/escript
> main([Arg1, Arg2]) ->
>     F = fun(Arg1) -> match;                                                                                                                                                                 
>            (_) -> nomatch                                                                                                                                                                    
>         end,                                                                                                                                                                                 
>     io:format("~p~n", [F(Arg2)]).

Compiling this (R13B04) will complain about
./test/funbind.es:3: Warning: variable 'Arg1' is unused
./test/funbind.es:4: Warning: variable 'Arg1' is unused
./test/funbind.es:4: Warning: variable 'Arg1' shadowed in 'fun'
and create a fun where Arg1 is unbound. 

If I change the implementation to :
>    F = fun(Arg1_tmp) when Arg1_tmp == Arg1 -> match;                                                                                                                                         
>           (_) -> nomatch                                                                                                                                                                      
>        end,                                                                                                                                                                                 
or even :
>    F = fun(Foo) ->
>                case Foo of
>                    Arg1 -> match;
>                    _ -> nomatch  
>                end
>        end,
I'll get the behavior I want (creating the fun with Arg1 bound) without any 
compile warning.


Is this the expected behaviour ? If so, what's the rationale ? It's easy to 
work around, but it is quite surprising.

-- 
Vincent de Phily
Mobile Devices
+33 (0) 142 119 325
+353 (0) 85 710 6320 

Warning
This message (and any associated files) is intended only for the use of its
intended recipient and may contain information that is confidential, subject
to copyright or constitutes a trade secret. If you are not the intended
recipient you are hereby notified that any dissemination, copying or
distribution of this message, or files associated with this message, is
strictly prohibited. If you have received this message in error, please
notify us immediately by replying to the message and deleting it from your
computer. Any views or opinions presented are solely those of the author
 and do not necessarily represent those of 
the
company. Although the company has taken reasonable precautions to ensure no
viruses are present in this email, the company cannot accept responsibility
for any loss or damage arising from the use of this email or attachments.


More information about the erlang-questions mailing list