switch to member
Ignacious via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Jan 13 19:20:24 PST 2017
When doing common functionality for a switch, is there any way to
optimize:
switch(x)
{
case X:
q.X = e;
break;
case Y:
q.Y = e;
break
etc...
}
e is basically a value that, depending on the what kind(x), we
assign it to a field in q. The name of the field and the case
label are identical(in fact, it wouldn't hurt to do it either
automatically(if x's enum name, if exists, matches a field, auto
assign) or create the field in the class q automatically if the
label matches a certain range or set).
Basically the idea is to avoid duplicating a lot of code.
I imagine one could write a string mixin that generates the cases
and assignments but I'm hoping for a more elegant solution.
More information about the Digitalmars-d-learn
mailing list