Missing Scope Rules Description in Erlang Reference Manual

Yao Bao free7by@REDACTED
Sun Sep 19 01:34:40 CEST 2021


That’s complicated....

Perhaps the best way to understand them is to examine the scope rules when my code has to write in those ways....

Thanks!

Yao

> 在 2021年9月17日,11:32,Chen Slepher <cxyfree@REDACTED> 写道:
> 
> 
> It’s a little complicated.
> 
> there is many clauses with many scopes in one function, or one case or others, we call it neighbor clause scope.
> 
> local scope is based on clauses or comprehensions
> there is two type of local scope
> shadowed and non_shadowed
> for function clause, named function clause
> the scope rule is shadowed:
> binding in pattens like function parameter, named function name, will shadow previous binding outside scope
> new binding in expressions will not seen outside scope or in neighbor clause scope, it could be used repeatedly
> ```
> fun(A) -> A1 = A + 1, A1 end,
> fun Name(A) -> A1 = Name(A -1), A1; (0) -> A1 = 0, A1 end
> [A || A <- As]
> ```
> for comprehension
> the scope rule is also shadowed but with two level: comprehension and generate
> binding in pattens in comprehension generate, will not only shadow previous binding outside comprehension, and also shadow previous binding inside comprehension
> new binding in expression in comprehension generate not seen outside comprehension generate
> new binding in expression in comprehension without generate not seen outside comprehension, but usable in next expressions in comprehension
> 
> ```
> test_lc(A) ->
>     A_1 = [{A_3, B}
> 	   || B <- [begin A_1 = A + 2, A_1 end],
> 	      begin A_1 = A + 3, A_1, true end,
> 	      A_2 <- [begin A_2 = A_1 + 1, A_2 end],
>               is_ok(A_2 + 3, begin A_3 = A_2 + 4, A_3 end),
> 	      true],
>     A_1.
> ```
> for case clause, if clause, try catch clause,  receive after clause
> the scope rule is non-shadowed:
> binding in clause pattens is same as previous binding outside scope
> new binding in expressions will not seen in neighbor clause scope, it could be used repeatedly
> but outside soup, unless the binding with same name is bind in all neighbor clause scope, you will get an compile time unsafe error
> example:
> ```
> test(A, A1) ->
>     case A of
>         A1 ->
>             A2 = 3,
>             A3 = 2,
>             A3;
>         A  ->
>             A2 = 4,
>             A2
>     end,
>     A2,
>     A3. %% variable 'A3' unsafe in 'case'
> ```
> 
> ```
> hello(A = 1, A1 = 2)
> [A = 1|[A1 = 2]]
> {A = 1, A1 = 2}
> #hello{world1 = (A = 1), world2 = (A1 = 2)}
> #{world1 => (A = 1), world2 => (A1 = 2)}
> (A = 1) + (A1 = 2)
> ```
> these expression has it's scope group
> for this scope, we name it argument scope
> every variable binded in scope could not used in neighbor scope, but avaliable outside scope group
> for example
> ```
> 1> (A = A1) + (A1 = 2).
> * 1: variable 'A1' is unbound
> 1> (A = 1) + (A1 = A).
> * 1: variable 'A' is unbound
> 1> ((A = 1) + (A1 = 2)), A1.
> 2
> ```
> but usually user don't write code this style, you could just ignore it.
> 
> Previous conclusion is studied by test in erlang-otp R23.
> 
>> 2021年9月17日 08:23,Yao Bao <free7by@REDACTED> 写道:
>> 
>> Hello,
>> 
>> I'm trying to understand the scope rules which Erlang has.
>> 
>> As far as I know, Erlang has no global scope (data), and the local scope is based on function clauses. Is there anything else related to scope rules but I am not aware of?
>> 
>> I explored the documentation, but cannot find the description/definition of scope rules in it (Erlang Reference Manual).
>> If we missed this description/definition, should we add it into the reference manual?
>> 
>> Yao
>> 
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210919/548acea0/attachment.htm>


More information about the erlang-questions mailing list