Bug 19032 – Alias this does not interact with inheritance.
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-06-27T23:44:50Z
Last change time
2018-06-28T06:34:13Z
Assigned to
No Owner
Creator
Igor Stepanov
Comments
Comment #0 by wazar.leollone — 2018-06-27T23:44:50Z
This case doesn't properly work.
interface IShort
{
@property ref short getShort();
alias getShort this;
}
interface IString
{
@property ref string getString();
alias getString this;
}
interface IDouble
{
@property ref double getDouble();
alias getDouble this;
}
class Test : IShort, IString, IDouble
{
short a;
string b;
double d;
this(short a, string b, double d)
{
this.a = a;
this.b = b;
this.d = d;
}
override @property ref short getShort()
{
return a;
}
override @property ref string getString()
{
return b;
}
override @property ref double getDouble()
{
return d;
}
}
void test9()
{
Test t = new Test(1, "2", 3.0);
short a = t;
string b = t;
double d = t;
assert(a == 1);
assert(b == "2");
assert(c == 3.0);
}
Comment #1 by simen.kjaras — 2018-06-28T06:33:34Z
Simplified:
class A {
int n;
alias n this;
}
class B : A {}
unittest {
// Works when accessed through an A:
static assert(__traits(compiles, cast(int)new A()));
static assert(__traits(compiles, cast(int)cast(A)new B()));
// Fails when accessed through a B:
static assert(!__traits(compiles, cast(int)new B()));
}
OP also requires multiple alias this, which the documentation states is currently unimplemented (issue 6083).
Comment #2 by simen.kjaras — 2018-06-28T06:34:13Z
*** This issue has been marked as a duplicate of issue 5380 ***