Operator overloading through UFCS doesn't work

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 25 14:50:06 PDT 2016


On Wednesday, May 25, 2016 15:46:23 Elie Morisse via Digitalmars-d-learn 
wrote:
> On Tuesday, 24 May 2016 at 23:43:46 UTC, Jonathan M Davis wrote:
> > On Tuesday, May 24, 2016 23:19:32 Elie Morisse via
> >
> > Digitalmars-d-learn wrote:
> >> On Saturday, 13 October 2012 at 22:58:56 UTC, Timon Gehr wrote:
> >> > Afaik free-function operator overloads (but not in the
> >> > context of UFCS) were considered and turned down because D
> >> > did not want to get amidst discussions about adding Koenig
> >> > lookup. UFCS does not do Koenig lookup.
> >>
> >> I don't get it, aren't the current symbol lookup rules enough
> >> to make free function operator overloads useful? To me it
> >> looks like they are.
> >>
> >> Sorry for digging up this thread, just getting irritated by a
> >> restriction that seems pointless and arbitrary.
> >>
> >> Overloaded operators would suffer from the same potential
> >> abuses other methods are subjected to if UFCS was enabled,
> >> nothing more as far as I can see.
> >
> > If UFCS doesn't work, because there are two free functions with
> > the same name which take the same arguments, then you can
> > differentiate between them by not using UFCS and using their
> > full import paths, or you can alias them so that they don't
> > have the same name. Neither of those would be possible with
> > operator overloading. If overloaded operators were allowed as
> > free functions, then if there were ever a symbol conflict,
> > you'd be screwed.
> >
> > - Jonathan M Davis
>
>    X.FreeFunc(Y); // multiple matches error
>    ModuleA.FreeFunc(X, Y); // ok
>
>    X * Y; // multiple matches error
>    ModuleA.opBinary!'*'(X, Y); // ok
>
> Is there much of a difference between the two?

It's not an overloaded operator anymore at that point, and that definitely
fails to work for generic code, since not all operators are overloaded
operators. Free functions don't have that problem.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list