Thread overview
[Issue 8062] New: UFCS and operator overloading
May 31, 2012
Dmitry Olshansky
Jun 13, 2012
timon.gehr@gmx.ch
Jun 13, 2012
Dmitry Olshansky
Jun 13, 2012
timon.gehr@gmx.ch
May 08, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8062

           Summary: UFCS and operator overloading
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: jens.k.mueller@gmx.de


--- Comment #0 from jens.k.mueller@gmx.de 2012-05-08 01:08:27 PDT ---
According to TDPL (p. 388, Table 12.1)
a + b
is rewritten to
a.opBinary!("+")(b) or b.opBinaryRight!("+")(a)

Considering operator overloading together with UFCS leads to the following wrong behavior.

struct Foo {}

Foo opBinary(string op)(Foo lhs, Foo rhs) if (op == "+")
{
    return Foo.init;
}

unittest
{
    Foo a, b;
    a + b; // fails to compile even though it should
}

If a + b is rewritten to a.opBinary!("+")(b) first, then UFCS should rewrite it
further to opBinary!("+")(a,b). But it seems UFCS is not considered. UFCS
should be carried out when the operator is being rewritten.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 31, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8062


Dmitry Olshansky <dmitry.olsh@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dmitry.olsh@gmail.com


--- Comment #1 from Dmitry Olshansky <dmitry.olsh@gmail.com> 2012-05-31 06:21:54 PDT ---
As defined t shouldn't:
UFCS presently only does go from fn(a,b, ...) -> a.fn(b, ...); way.

And arguably rightfully so, as it already may introduce some hijacking.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8062


timon.gehr@gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr@gmx.ch


--- Comment #2 from timon.gehr@gmx.ch 2012-06-13 12:18:57 PDT ---
(In reply to comment #1)
> As defined t shouldn't:
> UFCS presently only does go from fn(a,b, ...) -> a.fn(b, ...); way.
> 

Well, no. UFCS transforms a.fn(b, ...) to fn(a,b, ...).

> And arguably rightfully so, as it already may introduce some hijacking.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8062



--- Comment #3 from Dmitry Olshansky <dmitry.olsh@gmail.com> 2012-06-13 12:22:05 PDT ---
(In reply to comment #2)
> (In reply to comment #1)
> > As defined t shouldn't:
> > UFCS presently only does go from fn(a,b, ...) -> a.fn(b, ...); way.
> > 
> 
> Well, no. UFCS transforms a.fn(b, ...) to fn(a,b, ...).

My bad, I actually meant it a.fn ---> fn(a,...) way. Which brings us to the point  - request is Invalid then?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8062



--- Comment #4 from timon.gehr@gmx.ch 2012-06-13 12:37:21 PDT ---
(In reply to comment #3)
> 
> My bad, I actually meant it a.fn ---> fn(a,...) way. Which brings us to the point  - request is Invalid then?

I don't think it is invalid.

a + b -> a.opBinary!"+"(b) -> opBinary!"+"(a, b);

Probably this is on the borderline between bug and enhancement request -- I think the documentation can be interpreted either way.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8062



--- Comment #5 from jens.k.mueller@gmx.de 2012-06-13 14:05:28 PDT ---
(In reply to comment #4)
> (In reply to comment #3)
> > 
> > My bad, I actually meant it a.fn ---> fn(a,...) way. Which brings us to the point  - request is Invalid then?
> 
> I don't think it is invalid.
> 
> a + b -> a.opBinary!"+"(b) -> opBinary!"+"(a, b);
> 
> Probably this is on the borderline between bug and enhancement request -- I think the documentation can be interpreted either way.

Which documentation are you referring to? I don't think this is an enhancement. How can it be an enhancement? Are there exceptions when UFCS has to be applied?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------