Using . notation abstractly
James Japherson
JJ at goolooking.com
Wed Oct 10 22:56:14 UTC 2018
struct Dispatcher(Base...)
{
static auto opDispatch(string name, T...)(T vals)
{
pragma(msg, " - ", name);
return Dispatcher!(Base, name).init;
}
}
struct Dispatch
{
alias X = Dispatcher!void;
alias X this;
}
pragma(msg, Dispatch.A.B.C, " - ", typeof(Dispatch.A.B.C));
CT Output:
- A
- B
- C
Dispatcher() - Dispatcher!(void, "A", "B", "C")
The idea is one can do
Dispatch.Left.Right.A.B.C;
and it will sequentially handle all the indirections regardless
of how many and return an object that encodes the path. (which
then can be collapsed down to a single string).
Ultimately some action can then be taken on the path.
The point of all this is because D does not allow nesting of enums
which allows for nice use of . to separate hiearchies:
enum A
{
enum B
{
X,
}
}
A.B.X, rather than having to have one large flat enum and do
things like A_B_X.
I know one can use structs but it is messy and not general enough.
Dispatch.A.B.X
can be used such as
struct A
{
alias Dispatch this;
}
A.B.X
but it requires more machinery to setup.
The purpose is mainly to be able to hijack the `.` notation to
create nice separations visually like we do for indirection but
allow it to mean many different things.
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?
More information about the Digitalmars-d-learn
mailing list