Operator overloading -- lets collect some use cases

Christopher Wright dhasenan at gmail.com
Sun Dec 28 11:42:36 PST 2008


Don wrote:
> 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.
> 

I've seen | and & used for fluent interfaces in C#:
Assert.That(0, Is.GreaterThan(-1) & Is.LessThan(1));

That is a bit awkward, a bit forced. In this situation, you want to use 
&& or ||, which is possible by defining an implicit cast to bool in C#, 
but D wouldn't use that cast (I think, hopefully I'm wrong). This would 
mess up autocomple-- er, wait, that term isn't backed by Microsoft's 
army of lawyers. It would mess up Microsoft(r) Intellisense(tm).



More information about the Digitalmars-d mailing list