Implicit enum conversions are a stupid PITA

Adam D. Ruppe destructionator at gmail.com
Thu Mar 25 12:57:27 PDT 2010


On Thu, Mar 25, 2010 at 03:34:06PM -0400, Nick Sabalausky wrote:
> I haven't used opDispatch, and don't remember much from the discussions of 
> it, so pardon a possibly dumb question: Would that prevent you from getting 
> a (meaningful) compile-time error if you mis-typed one of the flags?

A static assert would fire if the flag name given isn't in its internal table.

The opDispatch function takes a string. It'd look something like this:

====
import std.stdio;

struct Flags(T) {
	int opDispatch(string name)() {
		foreach(item; itemList) {
			if(name == item.name)
				return item.value;
		}

		static assert(0, "No such member: " ~ name);
	}

	struct Item {
		string name;
		T value;
	}

	Item[] itemList;
}


void main() {
	Flags!int a;

	int b = a.what;
}

===

$ dmd -c test.d
test.d(10): Error: static assert  "No such member: what"


The actual implementation of the Flags struct would be different to keep
it all compile time (and of course, offer a constructor of sorts), but this
show the idea and the error you can get.

-- 
Adam D. Ruppe
http://arsdnet.net



More information about the Digitalmars-d mailing list