Debug help - delegate from dlang Tour

Brother Bill brotherbill at mail.com
Mon Sep 8 16:46:01 UTC 2025


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.

Yep, as a nested function, this is a delegate, not a function, so 
incompatible.

The following code works:
```
import std.stdio;

void main()
{
	foo();
}

// Global function.  Doesn't work if nested non-static function
int add(int lhs, int rhs)
{
	return lhs + rhs;
}

void foo()
{
	int doSomething(int function(int, int) doer)
	{
		// call passed function
		return doer(5, 6);
	}

	doSomething(&add);
}
```


More information about the Digitalmars-d-learn mailing list