Thread overview
[Issue 5893] New: Allow simple aliases for operator overloading
Jan 22, 2012
Andrej Mitrovic
Jan 22, 2012
timon.gehr@gmx.ch
Jun 07, 2012
Kenji Hara
Feb 05, 2013
Andrej Mitrovic
Feb 06, 2013
Kenji Hara
Feb 06, 2013
Andrej Mitrovic
April 26, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5893

           Summary: Allow simple aliases for operator overloading
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: schveiguy@yahoo.com


--- Comment #0 from Steven Schveighoffer <schveiguy@yahoo.com> 2011-04-26 13:04:35 PDT ---
If you wish to use virtual operators in D classes, the current thinking is you would wrap a virtual function with a template function that handles the operator.  This means you have to implement a template function, taking care to use the same arguments that the other function will require, and using the proper return type as well.

I thought of an alternative way to do this that makes wrapping virtual functions super-easy.  However, the compiler rejects it (for a strange reason).

class C
{
   void concatAssign(C other) { }
   void concatAssign(int other) { } // to demonstrate overloading

   template opOpAssign(string s) if (s == "~=")  // line 6
   { alias concatAssign opOpAssign; }
}

void main()
{
   auto c = new C;
   c.opOpAssign!"~="(c); // works
   c.opOpAssign!"~="(1); // works
   c ~= 1; // line 15
}

testaliasvirt.d(6): Error: template testaliasvirt.C.opOpAssign(string s) if (s
== "~=") is not a function template
testaliasvirt.d(15): Error: cannot append type int to type testaliasvirt.C

I think the compiler should accept this code.  In fact, I thought the compiler just rewrote ~= to .opOpAssign!"~=".  The error message seems to suggest it does some unnecessary checking to make sure it's a function template.

If it did work, this method provides not only a very efficient and backwards compatible way to do operators (no wrapper code is generated/called, no relying on inlining to make sure performance isn't hurt), but one could easily create a single mixin that forwarded all operators to the old style operators (if you didn't want to do it manually like I have).  You don't even need to deal with forwarding parameters (the biggest pain of auto-wrapping functions)!

I can see this being of use for opDispatch as well.

Note that bug 4182 would have to be fixed for true backward compatibility (i.e. forwarding covariance with the virtual operators).  But that technically is a separate issue, not a blocker.

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


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-01-21 18:19:18 PST ---
Hows this for a workaround:

import std.traits;

class C
{
    void concatAssign(C other) { }
    void concatAssign(int other) { }

    auto ref opOpAssign(string s, T)(T t) if (s == "~")
    {
        static if (is(ReturnType!concatAssign == void))
            concatAssign(t);
        else
            return concatAssign(t);
    }
}

void main()
{
    auto c = new C;
    c.opOpAssign!"~"(c); // works
    c.opOpAssign!"~"(1); // works
    c ~= 1;              // works
}

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


timon.gehr@gmx.ch changed:

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


--- Comment #2 from timon.gehr@gmx.ch 2012-01-21 18:35:23 PST ---
1. The static if in the function body is not required. You can return a void
expression from a void function.
2. Here is what still does not work:

class C{
    void concatAssign(C other) { }
    void concatAssign(short other) { }
    void concatAssign(short[] other) { }

    auto ref opOpAssign(string s, T)(T t) if (s == "~"){
        return concatAssign(t);
    }
}

void main()
{
    auto c = new C;
    c.opOpAssign!"~"(c); // works
    c.opOpAssign!"~"(1); // no go
    c ~= 1;              // no go
    c ~= [1,2,3];        // no go

    // (compare to)
    short[] s;
    s ~= 1;       // works
    s ~= [1,2,3]; // works
}

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


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull


--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> 2012-06-07 00:03:19 PDT ---
https://github.com/D-Programming-Language/dmd/pull/989

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



--- Comment #4 from github-bugzilla@puremagic.com 2012-11-22 06:24:01 PST ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/8bc59cfe8e6896435f20ce1e9bdcc226942f59a8 fix Issue 5893 - Allow simple aliases for operator overloading

https://github.com/D-Programming-Language/dmd/commit/2888ec45218e2e49048e9f96be2fa0481dc00e31 Merge pull request #989 from 9rnsr/fix5893

Issue 5893 - Allow simple aliases for operator overloading

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=5893



--- Comment #5 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-02-05 13:27:26 PST ---
(In reply to comment #4)
> Commits pushed to master at https://github.com/D-Programming-Language/dmd
> 
> https://github.com/D-Programming-Language/dmd/commit/8bc59cfe8e6896435f20ce1e9bdcc226942f59a8 fix Issue 5893 - Allow simple aliases for operator overloading
> 
> https://github.com/D-Programming-Language/dmd/commit/2888ec45218e2e49048e9f96be2fa0481dc00e31 Merge pull request #989 from 9rnsr/fix5893
> 
> Issue 5893 - Allow simple aliases for operator overloading

The OP sample still fails.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 06, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=5893


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #6 from Kenji Hara <k.hara.pg@gmail.com> 2013-02-05 17:24:03 PST ---
(In reply to comment #5)
> (In reply to comment #4)
> > Commits pushed to master at https://github.com/D-Programming-Language/dmd
> > 
> > https://github.com/D-Programming-Language/dmd/commit/8bc59cfe8e6896435f20ce1e9bdcc226942f59a8 fix Issue 5893 - Allow simple aliases for operator overloading
> > 
> > https://github.com/D-Programming-Language/dmd/commit/2888ec45218e2e49048e9f96be2fa0481dc00e31 Merge pull request #989 from 9rnsr/fix5893
> > 
> > Issue 5893 - Allow simple aliases for operator overloading
> 
> The OP sample still fails.

The OP sample cannot compile, because ConcatAssignExp will try to instantiate opOpAssign!("~"). Correct sample code is:

class C
{
   void concatAssign(C other) { }
   void concatAssign(int other) { } // to demonstrate overloading

   template opOpAssign(string s) if (s == "~")  // FIXED
   { alias concatAssign opOpAssign; }
}
void main()
{
   auto c = new C;
   c.opOpAssign!"~"(c); // works
   c.opOpAssign!"~"(1); // works
   c ~= 1; // line 15
}

So, this bug is already fixed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 06, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=5893



--- Comment #7 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-02-05 17:33:07 PST ---
(In reply to comment #6)
>    template opOpAssign(string s) if (s == "~")  // FIXED

This is actually worthy of an enhancement. If "~=" is found in opOpAssign the compiler should inform the user to use "~" instead. The same goes for +=, etc.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 06, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=5893



--- Comment #8 from Steven Schveighoffer <schveiguy@yahoo.com> 2013-02-06 08:55:06 PST ---
(In reply to comment #6)
> (In reply to comment #5)
> > The OP sample still fails.
> 
> The OP sample cannot compile, because ConcatAssignExp will try to instantiate
> opOpAssign!("~").

In my defense, I believe that originally it DID work the way I specified, whether it be by design or by accident ;)

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