Templatized delegates

Andrey Zherikov andrey.zherikov at gmail.com
Wed Jun 1 00:46:01 UTC 2022


On Tuesday, 31 May 2022 at 23:15:24 UTC, Andrey Zherikov wrote:
> On Tuesday, 31 May 2022 at 21:53:17 UTC, Paul Backus wrote:
>> If you want compile-time polymorphism, three's no other way.
>
> Yes, I want compile-time polymorphism.

In case if `S.doSomething` is NOT template function then the 
problem can be solved easily by wrapping access to `S` into 
another delegate:

```d
import std.stdio: writeln;

struct S
{
     void doSomething(int value) { value.writeln; }
}

alias DG = void delegate (void delegate(int) doSomething);

auto createDG(T)(T value)
{
     return delegate (void delegate(int) doSomething) { 
doSomething(value); };
}

struct R
{
     DG dg;

     this(int value)
     {
         dg = createDG(value);
     }
}

void main()
{
     auto r = R(5);

     S s;
     r.dg(_ => s.doSomething(_));
}
```

How to do the same for templated `S.doSomething`?


More information about the Digitalmars-d-learn mailing list