Two part question. Making a dynamic array of delegates, and taking in a delegate with unknown parameters as an argument .
Mike Parker via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Dec 1 16:23:33 PST 2016
On Thursday, 1 December 2016 at 23:51:19 UTC, Payotz wrote:
> The register method will take in delegates as an argument, but
> those delegates have varied arguments themselves, so I can't
> really put anything there. I know that it's got something to do
> with templates so I tried my hand in it and came up with this:
>
> void registerEvent(string event_name,T...)(T delegate() dg);
Off the top of my head, you should be able to do something like
this:
void registerEvent(string event_name, T, Args...)(T
delegate(Args) dg) {}
>
> I know there's something missing in the way I did it, so I'll
> be glad for you folks to tell me what I'm missing.
>
> And for the second part of the question, I can't seem to make a
> Dynamic Array where I could store the delegates taken in the
> "registerEvent" method. Closest thing I have gotten to is this:
>
> private void delegate(T)(T args)[string] event;
>
> which resulted in the compiler screaming Error signs at me.
> So how does one do it?
This isn't going to work because you can't have an array of mixed
types. Something like `KyeEvent delegate() dg` and `MouseEvent
delegate() dg` are distinct types, so they can't be stored in the
same array.
Since you say the delegates have different parameters, the only
option I'm aware of to store them is to use either the Variant or
Algebraic type in std.variant. If only the return type differed,
there would be other options. And I don't know what you're doing
with that event_name template parameter. But anyway, checkout
std.variant.
More information about the Digitalmars-d-learn
mailing list