Puzzled by this behavior

Steven Schveighoffer schveiguy at gmail.com
Tue May 31 20:24:17 UTC 2022


On 5/31/22 4:11 PM, Ola Fosheim Grøstad wrote:
> On Tuesday, 31 May 2022 at 19:59:21 UTC, Steven Schveighoffer wrote:
>> Note that you can declare a prototype, but this also declares a 
>> symbol, and D does not allow you to redefine symbols.
> 
> And this doesn't make any sense. Why would local functions not work like 
> lambdas?

Because they aren't lambdas. To use your example:

```d
void main()
{
     int i;
     void foo(); // declares foo with no definition

     auto bar = (){
         writeln("Hello world");
         if(++i < 10) foo();
     };

     void foo() {bar(); }; // redeclares foo, not allowed

     foo();
}
```

would be the same with lambdas as:

```d
void main()
{
     int i;
     void delegate() foo; // declares foo and assigns it to null

     auto bar = (){
         writeln("Hello world");
         if(++i < 10) foo();
     };

     //foo = (){ bar(); }; // this is NOT a declaration, it's an assignment
     void delegate() foo = () {bar(); }; // redeclares foo, not allowed

     foo();
}
```

You can't "assign" functions like you can lambdas. And D does not allow 
redeclaring a symbol of any type in a specific scope.

-Steve


More information about the Digitalmars-d mailing list