Debug help - delegate from dlang Tour

Brother Bill brotherbill at mail.com
Mon Sep 8 16:23:23 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.

Marking add static works.

Still don't understand why this doesn't work.
```
import std.stdio;

void main()
{
	foo();
}

void foo() {
	int addMyInts(int lhs, int rhs) {
		return lhs + rhs;
	}

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

	doSomething(&addMyInts);
}
```

Console output
```
c:\dev\D\D_templates_tutorial\toy1\source\app.d(19): Error: 
function `doSomething` is not callable using argument types `(int 
delegate(int lhs, int rhs) pure nothrow @nogc @safe)`
     doSomething(&addMyInts);
                ^
c:\dev\D\D_templates_tutorial\toy1\source\app.d(19):        
cannot pass argument `&addMyInts` of type `int delegate(int lhs, 
int rhs) pure nothrow @nogc @safe` to parameter `int 
function(int, int) doer`
c:\dev\D\D_templates_tutorial\toy1\source\app.d(13):        
`app.foo.doSomething(int function(int, int) doer)` declared here
     int doSomething(int function(int, int) doer)
         ^
```



More information about the Digitalmars-d-learn mailing list