[erlang-questions] is there "return" in Erlang.

Frédéric Trottier-Hébert fred.hebert@REDACTED
Mon Feb 28 15:48:15 CET 2011



On 2011-02-28, at 09:18 AM, Jachym Holecek wrote:

> # Attila Rajmund Nohl 2011-02-28:
> 
> So "special" constructs in Erlang, like 'case' or 'if', are really just
> syntactic sugar on top of lambdas (the strange scoping rule of 'case'
> breaks the most straightforward approach to this, but can still be dealt
> with cleanly).
> 
> I think this is a very powerful property, although as you can see I have
> a bit of a trouble in clearly expressing why is that. ;-)
> 
> Take care,
> 	-- Jachym
> 
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
> 


This is not exactly true. The 'case ... of' expression has special scoping rules that do not fit those of lambdas or functions in general:

1> case 1 of _ -> R = 3 end.
3
2> R.
3

Compare with a fun/lambda:

3> (fun(_) -> D = 3 end)(1).
3
4> D.
* 1: variable 'D' is unbound

And the 'if' expression follows the same pattern. Moreover, in a fun, you will 'shadow' the variables in the head, meaning you'll overwrite them if they existed in the current context. A case expression won't -- using the same binding of 'R = 3' as above:

5> case 2 of R -> yay end.
** exception error: no case clause matching 2


From what I heard, the underlying dispatching principle is similar, but yeah. They're not equivalent in how they handle scoping or binding.


--
Fred Hébert
http://www.erlang-solutions.com



More information about the erlang-questions mailing list