Operator overloading -- lets collect some use cases

Bill Baxter wbaxter at gmail.com
Sun Dec 28 11:00:23 PST 2008


On Mon, Dec 29, 2008 at 1:50 AM, Don <nospam at nospam.com> 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.

Array-like types which implement
opCat/opCatAssign/opIndex/opIndexAssign/opSlice/opSliceAssign/opApply
(and multiple flavors of some of those.)
I think the biggest problem there is just the sheer number of methods
you have to implement to support the array concept.

Merging might be useful there too --- A ~= b ~ c ~ d  is probably more
efficiently implemented as 3 ~= ops.

--bb



More information about the Digitalmars-d mailing list