D casting broke?
Joerg Joergonson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Jun 19 14:19:10 PDT 2016
On Sunday, 19 June 2016 at 20:21:35 UTC, ag0aep6g wrote:
> On 06/19/2016 09:59 PM, Joerg Joergonson wrote:
>> This should be completely valid since B!T' obviously derives
>> from A!T
>> directly
>
> ok
>
>> and we see that T' derives from b which derives from a
>> directly.
>
> ok
>
>> So B!b is an entirely derived from A!a
>
> No. B!b is derived from A!b, not from A!a. `b` being derived
> from `a` does not make A!b derived from A!a.
why not? This doesn't seem logical!
> Here is the full inheritance tree:
>
> X
> ├─x
> │ └─a
> │ └─b
> ├─A!a
> └─A!b
> └─B!b
But b is derived from a. Your tree completely ignores under A.
> X
> ├─x
> │ └─a
> │ └─b
> ├─A!a
| \
└─A!b
└─B!b
Just because D doesn't understand this logical consistency
between inheritance doesn't mean D is right. (Hence, why D's type
system is broke)
In fact, the tree should look like this:
> X
> ├─x
> │ └─a
> │ └─b
> └─A!x
│ \
└─A!a
│ \
└─A!b
│ \
└─B!b
Basically you are treating A!a and A!b as if a and be have no
relationship. BUT THEY DO! If you don't take that into account
then your wrong.
Simply stating how D behaves is not proof of why it is right or
wrong.
This is very easy to see, check my other post using a Widget
Example and you will see that it is a logical extension.
D doesn't check parameter inheritance relationships properly. A!b
is a derivation of A!a.
import std.stdio;
class a { }
class b : a { }
class A(T : a)
{
T x;
}
void main(string[] argv)
{
auto _A = new A!a();
auto _C = new A!b();
auto p = cast(A!a)_C;
}
p is null. My example with B is irrelevant. The issue is with the
parameter.
As you can see, D thinks that A!b and A!a are completely
unrelated... as do you and arsd.
Do you seriously think this is the case? That
class b : a { }
and
class b { }
effectively mean the same with regards to A?
The whole problem comes about at this line:
auto p = cast(A!a)_C;
We are trying to cast `T x` in C, which is effectively `b x` to
`a x`.
Is that not possible to do? We do it all the time, right?
That is my point. D doesn't see that it can do this but it can,
if not, prove me wrong.
More information about the Digitalmars-d-learn
mailing list