switch to member

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 13 23:13:31 PST 2017


On 01/13/2017 07:20 PM, Ignacious wrote:
> 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.

Sounds like opDispatch may be useful.

Ali



More information about the Digitalmars-d-learn mailing list