Skip to content

Instantly share code, notes, and snippets.

@PetarKirov
Created December 8, 2021 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PetarKirov/f347e59552dd87c4c02d0ce87d0e9cdc to your computer and use it in GitHub Desktop.
Save PetarKirov/f347e59552dd87c4c02d0ce87d0e9cdc to your computer and use it in GitHub Desktop.
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