Thread overview
[Issue 8520] New: Simple "in"-constrained opBinaryRight in interface doesn't work
Aug 08, 2012
Simen Kjaeraas
Aug 08, 2012
Jonathan M Davis
August 07, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8520

           Summary: Simple "in"-constrained opBinaryRight in interface
                    doesn't work
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: alex@lycus.org


--- Comment #0 from Alex Rønne Petersen <alex@lycus.org> 2012-08-08 01:54:18 CEST ---
┌─[alexrp@zor] - [~/Projects/tests] - [2012-08-08 01:52:24]
└─[$] <> cat test.d
import std.stdio;

interface I
{
    int* opBinaryRight(string op : "in")(int i);
}

class C : I
{
    int* opBinaryRight(string op : "in")(int i)
    {
        return null;
    }
}

void main()
{
    I i = new C;
    int* p = 42 in i;
    writeln(p);
}
┌─[alexrp@zor] - [~/Projects/tests] - [2012-08-08 01:52:26]
└─[$] <> dmd test.d
test.o:test.d:function _Dmain: error: undefined reference to
'_D4test1I30__T13opBinaryRightVAyaa2_696eZ13opBinaryRightMFiZPi'
collect2: ld returned 1 exit status
--- errorlevel 1

It's my understanding that this is supposed to work since the opBinaryRight template is constrained specifically to "in". This bug makes operators in interfaces pretty unusable.

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


Simen Kjaeraas <simen.kjaras@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras@gmail.com


--- Comment #1 from Simen Kjaeraas <simen.kjaras@gmail.com> 2012-08-07 18:26:30 PDT ---
Your understanding is wrong - templates never go in the vtable.

The solution is to use NVI and forwarding:

interface I
{
    int* opBinaryRight_in(int i);

    int* opBinaryRight(string op : "in")(int i)
    {
        return opBinaryRight_in(i);
    }
}

class C : I
{
    int* opBinaryRight_in(int i)
    {
        return null;
    }
}

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



--- Comment #2 from Alex Rønne Petersen <alex@lycus.org> 2012-08-08 12:15:19 CEST ---
(In reply to comment #1)
> Your understanding is wrong - templates never go in the vtable.
> 
> The solution is to use NVI and forwarding:
> 
> interface I
> {
>     int* opBinaryRight_in(int i);
> 
>     int* opBinaryRight(string op : "in")(int i)
>     {
>         return opBinaryRight_in(i);
>     }
> }
> 
> class C : I
> {
>     int* opBinaryRight_in(int i)
>     {
>         return null;
>     }
> }

I could understand if the opBinaryRight template wasn't constrained to "in", but it is, so I see no reason why it cannot be in the vtable since it can only ever have one instance in a class.

The NVI solution works, but it's very ugly.

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


Alex Rønne Petersen <alex@lycus.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|major                       |enhancement


--- Comment #3 from Alex Rønne Petersen <alex@lycus.org> 2012-08-08 12:15:50 CEST ---
Changing to enhancement.

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


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmx.com


--- Comment #4 from Jonathan M Davis <jmdavisProg@gmx.com> 2012-08-08 10:11:34 PDT ---
> I could understand if the opBinaryRight template wasn't constrained to "in",
but it is, so I see no reason why it cannot be in the vtable since it can only ever have one instance in a class.

Because templated functions _never_ end up in the vtable. Mixed in templates may if they're fully instantiated when mixed in (I don't remember), but a templated function written directly in the class is _never_ virtual.

That doesn't necessarily mean that there are no situations where the language could be enhanced to make a templated function virtual if it's actually restricted enough (and this may be such a case), but as it stands, being a template is enough to make it non-virtual regardless of what it does or how constrained it is or isn't. I don't believe that even

int func()() { return 2; }

would be virtual, and that's pretty much as constrained as it gets.

Of course, if such a function _did_ get changed to virtual, then it would always defined, whereas right now it's only defined if used, which could be a definite negative, depending on the programmer's intent. So, there's defintely a tradeoff here even if it's possible to make some templated functions virtual.

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