What is :-) ?
evilrat
evilrat666 at gmail.com
Mon Nov 20 16:32:22 UTC 2023
On Monday, 20 November 2023 at 16:09:33 UTC, Antonio wrote:
>
> Is there any way to force D compiler to treat this
> "createCounter" declaration as **delegate** instead of
> **function**?
>
> ```d
> auto createCounter = (int nextValue) => () => nextValue++;
> ```
generally there is a way to tell the compiler specifically that
you want a delegate or a function, and additionally there is
`toDelegate` function from std.functional that could be useful in
some cases.
https://dlang.org/phobos/std_functional.html#toDelegate
the syntax for specifying delegate/function is smth like this,
IIRC return type can be omitted but for reference I write it here
as auto.
```d
// this is a function returning a delegate
auto createCounter(int nextValue) => auto delegate() =>
nextValue++;
// this is a function returning a function
auto createCounter(int nextValue) => auto function() =>
nextValue++;
```
More information about the Digitalmars-d-learn
mailing list