[erlang-questions] Using bound variables in a fun's function clause

Ahmed Omar <>
Mon Oct 18 15:38:38 CEST 2010


Hi Vincent,
Yes it's expected. It's about the variables scope
You can read more here:
http://www.erlang.org/doc/programming_examples/funs.html#id58554

On Mon, Oct 18, 2010 at 3:29 PM, Vincent de Phily <
> wrote:

> 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.
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:
>
>


-- 
Best Regards,
- Ahmed Omar
http://nl.linkedin.com/in/adiaa
Follow me on twitter
@spawn_think <http://twitter.com/#!/spawn_think>


More information about the erlang-questions mailing list