Debug help - delegate from dlang Tour
evilrat
evilrat666 at gmail.com
Mon Sep 8 14:42:01 UTC 2025
On Monday, 8 September 2025 at 14:32:32 UTC, Brother Bill wrote:
> https://tour.dlang.org/tour/en/basics/delegates
>
> This is so simple. What is D complaining about?
> Should this also work with a Template, as shown?
>
> ```
> import std.stdio;
>
> void main()
> {
> // auto add(T)(T lhs, T rhs)
> // {
> // return lhs + rhs;
> // }
>
> int add(int lhs, int rhs) {
> return lhs + rhs;
> }
>
> int doSomething(int function(int, int) doer)
> {
> // call passed function
> return doer(5, 6);
> }
>
> doSomething(&add);
>
> }
>
> ```
>
> Console output:
> ```
> c:\dev\D\D_templates_tutorial\toy1\source\app.d(20): Error:
> function `doSomething` is not callable using argument types
> `(int delegate(int lhs, int rhs) pure nothrow @nogc @safe)`
> doSomething(&add);
> ^
> c:\dev\D\D_templates_tutorial\toy1\source\app.d(20):
> cannot pass argument `&add` 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(14):
> `app.main.doSomething(int function(int, int) doer)` declared
> here
> int doSomething(int function(int, int) doer)
> ^
> ```
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.
More information about the Digitalmars-d-learn
mailing list