How to use labeled break in static foreach?

cc cc at nevernet.com
Fri Feb 14 06:41:02 UTC 2020


import std.meta;
enum A = AliasSeq!(1, 2, 3, 4);
THREELOOP: static foreach (idx, field; A) {
	static if (field == 3) {
		pragma(msg, "Got a 3!");
		break THREELOOP;
	}
	static if (idx == A.length - 1) {
		static assert(0, "Got no 3...");
	}
}

What I'd like to achieve in this example is for compilation to 
fail if the given tuple doesn't contain a matching item.  Is 
there an easy way to do this?  Trying to break out of the static 
foreach gives me
Error:enclosing label `THREELOOP` for `break` not found

In this reduced example, I was able to accomplish this by setting 
some enum within the successful comparison body and then checking 
e.g. static if (!__traits(compiles, FOUND)) but this breaks down 
once I start dealing with multiple levels of scope.


More information about the Digitalmars-d-learn mailing list