What is :-) ?

Antonio antoniocabreraperez at gmail.com
Mon Nov 20 08:47:34 UTC 2023


If I run this code

```d
import std.stdio;

void main(){
   auto next = Counter(10);
   next().writeln;
   next().writeln;
   next().writeln;

   // What is "next" function?
   writeln( "'next' is ", next );
   // What is "Counter" function?   This fails
   // writeln( "'Counter' is ", Counter );
}

auto Counter(int nextValue) => () => nextValue++;
```

Executing this code results in:

```
10
11
12
'next' is int delegate() pure nothrow @nogc @safe
```

Now, I uncomment the ```writeln( "'Counter' is ", Counter );``` 
line and compiler says

```
/home/antonio/Devel/topbrokers/whatsapp-srv/admin/x.d(12): Error: 
function `x.Counter(int nextValue)` is not callable using 
argument types `()`
/home/antonio/Devel/topbrokers/whatsapp-srv/admin/x.d(12):        
too few arguments, expected 1, got 0
```

I understand the problem with UFCS (``next`` is not using UFCS 
because it is a delegate defined in the own main() function,  and 
``Counter``` without () is treated as a function call because it 
is UFCS eligible )

- What is the way to do ```writeln``` work with ```Counter``` 
function the same way it works with ```next``` function?


More information about the Digitalmars-d-learn mailing list