constructing labels for static foreach inside switch inside foreach
bauss
jj_1337 at live.dk
Wed Jul 8 05:42:03 UTC 2020
On Wednesday, 8 July 2020 at 02:06:01 UTC, Steven Schveighoffer
wrote:
> OK, so I have a situation where I'm foreaching over a
> compile-time list of types. Inside the loop, I'm using a second
> loop over a set of input.
>
> Inside that loop, I'm using a switch on the input, and inside
> the switch, I'm foreaching over the type's members, to
> construct a switch that can handle member names (this is for
> serialization).
>
> If I encounter a certain name, then I want to break out of the
> inner loop (it's a while loop)
>
> So naturally, I have to use break statements with labels like:
>
> innerloop:
> while(haveMoreData)
> switchstmt:
> switch(nextDataElement) {
> static foreach(name; __traits(allMembers, T)) {
> case name:
> ... // handle it
> break switchstmt;
> }
> case "STOP":
> break innerloop;
> }
>
> Seems simple enough, except that this inner portion is
> unrolled, and if I have more than one type to run this on, I
> already have an "innerloop" label defined.
>
> Is there a way to define a label using a mixin or something? or
> do I have to wrap this in a function?
>
> Is there another way to approach this?
>
> -Steve
Unfortunately mixin does not support labels so I don't think you
can do what you want to do.
Kind of surprised me tbh
More information about the Digitalmars-d-learn
mailing list