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?

struct BigInt {
   BigInt opBinary(string op)(BigInt rhs) if (op == "-") {
      ...
   }
}

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).

It could if we decide to deprecate opCmp. I happen to like it; if you define a <= b for inclusion, people will think it's natural to also allow a < b for strict inclusion. But that's up for debate.

I'm not sure what the best way is. Classes have opEquals and opCmp so the question is - do we want structs to be somewhat compatible with classes or not? My personal favorite choice would be to go full bore with compile-time strings.


Andrei

Reply via email to