Thread overview
[Issue 5380] New: Subtyping with "alias this" doesn't mix with regular inheritance
Dec 27, 2010
Max Samukha
Dec 27, 2010
Walter Bright
Nov 28, 2012
Max Samukha
Nov 28, 2012
Max Samukha
December 27, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5380

           Summary: Subtyping with "alias this" doesn't mix with regular
                    inheritance
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: samukha@voliacable.com


--- Comment #0 from Max Samukha <samukha@voliacable.com> 2010-12-27 03:59:11 PST ---
class A
{
}

class B
{
    A a;
    alias a this;
}

class C : B
{
}

void main()
{
    A a = new C; // error
}

Error: cannot implicitly convert expression (new C) of type test.C to test.A

Since C is a subtype of B (via inheritance) and B is a subtype of A (via "alias
this"), C should be implicitly convertible to A.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 27, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5380


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com


--- Comment #1 from Walter Bright <bugzilla@digitalmars.com> 2010-12-27 13:33:28 PST ---
Yes, I see the problem. Never thought of that.

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



--- Comment #2 from Max Samukha <samukha@voliacable.com> 2012-11-28 07:16:16 PST ---
This one is blocking a design for QtD (and potentially other C++ projects using
single inheritance with interfaces) that should allow at the cost of relatively
small constant overhead to avoid:
1) unnecessary and undesirable allocations or lookups of wrapper objects for
object pointers returned by the C++ code.
2) bloating the library with virtual call dispatchers for objects that are
never inherited in D code.

Very roughly:

// Instead of classes, fat pointers are generated for C++ classes:
struct QObject
{
    bool isDWrapper;
    void* pointerToCppObject;

    // a virtual
    void foo() {
        if (isDWrapper)
            // dispatch to the D override
        else
            // make static call to C++
    }

    enum dispatchers = q{
        void foo() {
            // dispatch to C++
        }
    };
}

// Library
class Inherit(T)
{
    T ref_;
    alias ref_ this;
    mixin(T.dispatchers);

    this() { isDWrapper = true; }
}

// User
class MyObject : Inherit!QObject
{
    override void foo()
    {
        ...
        super.foo();
        ...
    }
}

// Some function taking a QObject
void bar(QObject obj)
{
    obj.foo();
}

void main()
{
    // Here is the problem: MyObject is not a subtype of QObject
    bar(new MyObject);
}

The Inherit template subtypes the struct and provides the implementation of the overrides dispatching calls to the C++ side. If the user does not inherit from a Qt class (which will be true for most classes), the overrides are never compiled into the library. When receiving an object from the C++ side, we do not need to allocated a wrapper objects, just initialize the fat pointer with the relevant information.

I am not sure whether the craziness of the above is going to make it into the project but a prototype I've created does work.

Now that we can link to 64-bit C++ libraries directly on windows, we *maybe* could try to use (and improve) extern(C++) interfaces in order to avoid the complications. I am still not sure what to do with 32-bit Windows and would rather give a try to the design described.

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


Max Samukha <samukha@voliacable.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |critical


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