Using . notation abstractly

Simen Kjærås simen.kjaras at gmail.com
Thu Oct 11 11:54:47 UTC 2018


On Wednesday, 10 October 2018 at 22:56:14 UTC, James Japherson 
wrote:
> One of the problems is connecting it with actual code that does 
> something depending on the path in a way that is general enough 
> to be used for a wide variety of problems.
>
> Any ideas on how this could be done?

My first idea is to have Dispatch take an alias parameter, and 
see if it's useful for anything:

struct Dispatch(alias Fn, Base...) {
     static auto opDispatch(string name, T...)(T vals) {
         return Dispatch!(Fn, Base, name).init;
     }
     alias _get = Fn!Base;
     alias _get this;
}

// join a list of stuff with _ between each element.
template join(T...) {
     import std.conv : to;
     static if (T.length == 0) {
         enum join = "";
     } else static if (T.length == 1) {
         enum join = T[0].to!string;
     } else {
         enum join = T[0].to!string ~"_"~join!(T[1..$]);
     }
}

unittest {
     assert(Dispatch!join.A.B.C == "A_B_C");
}

No idea what to use it for, but it's kinda nice, I guess. Very 
generic, too.

--
   Simen


More information about the Digitalmars-d-learn mailing list