www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Lambdas Scope

reply Salih Dincer <salihdb hotmail.com> writes:
How is this possible? Why is it compiled? Don't the same names in 
the same scope conflict?

```d
int function(int) square;
void main()
{
   square = (int a) => a * a;
   int square = 5.square;
   assert(square == 25);
}
```

Thanks, SDB 79
Apr 11 2022
next sibling parent user1234 <user1234 12.de> writes:
On Monday, 11 April 2022 at 09:11:06 UTC, Salih Dincer wrote:
 How is this possible? Why is it compiled? Don't the same names 
 in the same scope conflict?

 ```d
 int function(int) square;
 void main()
 {
   square = (int a) => a * a;
   int square = 5.square;
   assert(square == 25);
 }
 ```

 Thanks, SDB 79
you can use explicit call parenthesis to make the difference between the local var and the global func. Also you have the module access operator and the fully qualified name as alternatives to select the one you wish to.
Apr 11 2022
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 11.04.22 11:11, Salih Dincer wrote:
 How is this possible? Why is it compiled? Don't the same names in the 
 same scope conflict?
 
 ```d
 int function(int) square;
 void main()
 {
    square = (int a) => a * a;
    int square = 5.square;
    assert(square == 25);
 }
 ```
 
 Thanks, SDB 79
- Local variables in a function can hide variables in less nested scopes. (Such hiding is only an error for nested block scopes within the same function.) - UFCS always looks up names in module scope, it does not even check locals.
Apr 11 2022