Call method with Variant array as parameters

Andre Pany andre at s-e-a-p.de
Sat Jul 14 14:04:25 UTC 2018


On Saturday, 14 July 2018 at 11:37:20 UTC, Timoses wrote:
> On Saturday, 14 July 2018 at 11:08:21
>
> How about this?
>
>
> import std.variant: Variant;
> import std.traits : isCallable;
>
> class Foo
> {
>     void bar(string s, long l)
>     {
>         import std.stdio : writeln;
>         writeln(s); writeln(l);
>     }
> }
>
> void call(T)(T fun, in Variant[] args)
>          if (isCallable!fun)
> {
>     import std.traits : Parameters;
>     alias Params = Parameters!fun;
>
>     Params params;
>     static foreach(i, param; params)
>     {
>         if (auto p = args[i].peek!(Params[i]))
>         {
>          	param = *p;
>         }
>         // perhaps create a warning if peeking was 
> unsuccessful...
>     }
>     fun(params);
> }
>
> unittest
> {
>     Foo foo = new Foo();
>     Variant[] args = [Variant("Hello"), Variant(42L)];
>     call(&foo.bar, args);
> }

Thank you so much!

Kind regards
Andre



More information about the Digitalmars-d-learn mailing list