Parameterized inheritence issues, bug or ignorance?

David Nadlinger via Digitalmars-d digitalmars-d at puremagic.com
Sun Jun 19 17:15:33 PDT 2016


On Monday, 20 June 2016 at 00:01:58 UTC, Joerg Joergonson wrote:
> class a { }
> class b : a { }
>
> class A(T : a)
> {
>    T x;
> }
>
>
>
> void main(string[] argv)
> {
> 	auto y = new A!a();
> 	auto z = new A!b();
> 	y.x = new a();
> 	z.x = new b();
>
>
> 	auto p1 = cast(A!a)y;
> 	auto p2 = cast(A!b)z;
> 	auto p3 = cast(A!a)z; // Fail! Why? z.x is of type b which can 
> be down cast to type a without a problem ->

As has been pointed out in dm.D.learn already (which is where 
this discussion belongs), there isn't any inheritance 
relationship between A!b and A!a.

In your example, it's easy to see that there can't be if you ask 
yourself whether the purported relation should be co- or 
contravariant in nature. To put it in simple terms, if your cast 
succeeded, you could do this:

---
p3.x = new a;
// Now typeof(z.x) == b, but it points to an instance of a!
---

  — David


More information about the Digitalmars-d mailing list