[erlang-questions] why isn't scope in "begin end" block?

Richard A. O'Keefe <>
Mon Oct 12 01:02:12 CEST 2015


On 10/10/2015, at 10:59 pm, Dao Gui <> wrote:

> Sorry to my pool English
> Example:
> 
> -module(test).
> -export([test/0]).
> 
> test() ->
>     begin A = 1 end,
>     A = 2. %% match error
>     %io:format("~p~n", [A]). %normal output
> 
> The var A is in begin end scope, but it can visited by other scope.
> Why isn't erlang support this?

Because it doesn't.  It never has.  It is so documented.
There is no such thing as "begin end scope" in Erlang.

You might as well ask why %start...%finish in Atlas Autocode
and IMP aren't scopes; why do;...end; in PL/I and XPL and PL/M
aren't scopes; why begin...end is not a scope in Pascal.

| without this feature, I can't write the next code:
> -define(OUTPUT_3(A), begin [_,_,B|_]=A, io:format("~p~n", [B]) end).

You can get the *effect* you want:

-define(OUTPUT_3(A), fun ([_,_,B|_]) -> io:format("~p~n", [B]) end(A)).

In general, you can express
   let Pat1 = Exp1
       Pat2 = Exp2
       ...
       Patn = Expn
    in Body
   end
as
   fun (Pat1, Pat2, ..., Patn) -> Body end(Exp1, Exp2, ..., Expn)




More information about the erlang-questions mailing list