Short list with things to finish for D2

Travis Boucher boucher.travis at gmail.com
Wed Nov 18 09:02:45 PST 2009


bearophile wrote:
> Andrei Alexandrescu:
> 
>> * Encode operators by compile-time strings. For example, instead of the 
>> plethora of opAdd, opMul, ..., we'd have this:
>>
>> T opBinary(string op)(T rhs) { ... }
>>
>> The string is "+", "*", etc.
> 
> Can you show an example of defining an operator, like a minus, with that?
>

T opBinary(string op)(T rhs) {
	static if (op == "-") return data - rhs.data;
	static if (op == "+") return data + rhs.data;

	// ... maybe this would work too ...
	mixin("return data " ~ op ~ "rhs.data;");
}

I love this syntax over the tons of different operation functions. 
Makes it so much nicer, especially when supporting a bunch of different 
paramater types (vectors are a good example of this).

T opBinary(string op)(T rhs)
T opBinary(string op)(float[3] rhs)
T opBinary(string op)(float rx, ry, rz)
....

> In my set data structure I'd like to define "<=" among two sets as "is subset". Can that design allow me to overload just <= and >= ? (opCmp is not enough here).
> 
> Bye,
> bearophile



More information about the Digitalmars-d mailing list