Debug help - delegate from dlang Tour
Steven Schveighoffer
schveiguy at gmail.com
Mon Sep 8 22:41:13 UTC 2025
On Monday, 8 September 2025 at 16:23:23 UTC, Brother Bill wrote:
> On Monday, 8 September 2025 at 14:42:01 UTC, evilrat wrote:
>>
>> probably because you have declared nested function `add`
>> inside `main`, this creates a delegate closure capturing
>> `main` scope, if you don't want that just mark `add` static.
>
> Marking add static works.
>
> Still don't understand why this doesn't work.
The easiest thing to do here is to declare it static, so the
compiler will reject any requirement of a context pointer.
I believe the case where it will infer a function vs. delegate is
when passing a literal lambda. Other than that, you have to be
explicit.
```d
void main() {
int foo1() { return 1; }
static int foo2() { return 1; }
int function() p;
p = &foo1; // error
p = &foo2; // ok
p = () { return 1; }; // ok, inferred to be a function, not
delegate
}
```
-Steve
More information about the Digitalmars-d-learn
mailing list