How to use labeled break in static foreach?

cc cc at nevernet.com
Fri Feb 14 06:48:50 UTC 2020


Here's a more involved example of what I'm trying to accomplish.  
Is there an easier/cleaner way to do this?  (This is still a bit 
reduced, what I'm actually trying to do is compare whether a 
given variadic typetuple passed to opDispatch is implicitly 
convertible to one of the parameter definitions of overloaded 
functions with the same name on another object).

import std.meta;
struct Test { int[3] x; }
enum A = Test([1, 3, 3]);
enum B = Test([5, 6, 7]);
enum C = Test([5, 6, 7]);
enum D = Test([8, 9, 0]);
enum ARRS = AliasSeq!(A, B, C, D);

enum TESTER = Test([5, 6, 7]);

static foreach (ai, af; ARRS) {
	static if (!__traits(compiles, FOUND)) {
		static foreach (idx, field; af.x) {
			// Have to declare a different MISMATCH enum for each inner 
loop iteration
			//   unless we enclose it in its own {} scope, but if we do 
that then FOUND
			//   is not visible to the outer loop
			static if (!__traits(compiles, mixin(format("MISMATCH_%d", 
ai)))) {
				static if (TESTER.x[idx] != af.x[idx]) {
					mixin(`enum bool `~format("MISMATCH_%d", ai)~` = true;`);
				}
			}
			static if (idx == af.x.length - 1 && !__traits(compiles, 
mixin(format("MISMATCH_%d", ai)))) {
				enum FOUND = ai;
			}
		}
	}
}
static if (__traits(compiles, FOUND)) {
	pragma(msg, format("entry #%d matches: %s", FOUND, ARRS[FOUND]));
} else {
	static assert(0, "Got no match...");
}



More information about the Digitalmars-d-learn mailing list