shortcut for dynamic dispatch and operators
Steven Schveighoffer
schveiguy at yahoo.com
Tue Dec 1 06:30:13 PST 2009
An idea I just had when thinking about how ugly opDispatch and opBinary
operators will be if we get those was, wouldn't it be cool if the compiler
could translate:
myTemplateMethod("abc" || "def")() if(condition) {}
to
myTemplateMethod(string __x)() if((__x == "abc" || __x == "def") &&
condition) {}
It makes dispatch based on compile-time strings much more palatable, for
example:
opDispatch("foo" || "bar")() {...}
opBinary("+" || "-" || "*")(int rhs) {...}
instead of:
opDispatch(string fn)() if(fn == "foo" || fn == "bar") {...}
opBinary(string op)() if(op == "+" || op == "-" || op == "*")(int rhs)
{...}
In fact, it can be generalized to any type which has literals:
factorial(int x)(){ return factorial!(x-1)() * x;}
factorial(1)() { return 1;}
What I don't know is if the || works in all cases -- because something
like true || false is a valid expression. Maybe someone can come up with
a better way.
-Steve
More information about the Digitalmars-d
mailing list