Operator overloading -- lets collect some use cases

Don nospam at nospam.com
Mon Dec 29 01:01:02 PST 2008

aarti_pl wrote:
> Don pisze:
>> There's been some interesting discussion about operator overloading 
>> over the past six months, but to take the next step, I think we need 
>> to ground it in reality. What are the use cases?
>> I think that D's existing opCmp() takes care of the plethora of 
>> trivial cases where <, >= etc are overloaded. It's the cases where the 
>> arithmetic and logical operations are overloaded that are particularly 
>> interesting to me.
>> The following mathematical cases immediately spring to mind:
>> * complex numbers
>> * quaternions (interesting since * is anti-commutative, a*b = -b*a)
>> * vectors
>> * matrices
>> * tensors
>> * bigint operations (including bigint, bigfloat,...)
>> I think that all of those are easily defensible.
>> But I know of very few reasonable non-mathematical uses.
>> In C++, I've seen them used for iostreams, regexps, and some stuff 
>> that is quite frankly bizarre.
>> So, please post any use cases which you consider convincing.
> DSL support in mother language. As an example I can give SQL in D or 
> mockup tests description language (usually also in D - not as a separate 
> script language).

Could you be more specific about this? For SQL, arithmetic and logical 
operators don't seem to be involved. The example you gave showed (if I 
understand correctly) a wish to make expression templates involving 
comparison operators, a task which is currently impossible.

I also found your first example a little too simple.

Query query = Select(a).Where(id == 5);

I presume you would also want to do things like:

for(int i=0; i<10; ++i) {
   Query query = Select(a).Where(id == (arr[i+2] + func(i)) || id==78+i);

meaning you'd also need to overload && and || operators.
Is that correct?

> In my previous posts I already put few arguments why it is sometimes 
> much more handy to use "DSL in mother language" approach rather than 
> string mixins with DSL language itself.

Still, that doesn't necessarily involve operator overloading. I really 
want to assemble a list of cases which do.

More information about the Digitalmars-d mailing list