Created
December 8, 2021 08:04
-
-
Save PetarKirov/f347e59552dd87c4c02d0ce87d0e9cdc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ICallable | |
{ | |
void opCall() const; | |
} | |
auto makeDelegate(alias fun, Args...)(auto ref Args args) | |
{ | |
return new class(args) ICallable | |
{ | |
Args m_args; | |
this(Args p_args) { m_args = p_args; } | |
void opCall() const { fun(m_args); } | |
}; | |
} | |
alias Action = void delegate(); | |
Action createDelegate(string s) | |
{ | |
import std.stdio; | |
return &makeDelegate!((string str) => writeln(str))(s).opCall; | |
} | |
struct A | |
{ | |
Action[] dg; | |
} | |
A create() | |
{ | |
A a; | |
a.dg ~= createDelegate("hello"); | |
a.dg ~= createDelegate("buy"); | |
return a; | |
} | |
void main() | |
{ | |
enum a = create(); | |
foreach(dg; a.dg) | |
dg(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment