D casting broke?

David Nadlinger via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 19 14:48:05 PDT 2016


On Sunday, 19 June 2016 at 21:06:43 UTC, Joerg Joergonson wrote:
> A!b is derived from A!a if b is derived from a, is it not? If 
> not, then I am wrong, if so then D casting has a bug.

You are wrong.

The array example given by Adam is actually a neat illustration 
of precisely your question if you just think of `T[]` as 
`Slice!T`. In other words, `Slice!b` cannot be derived from 
`Slice!a` automatically, since then you could use the `Slice!a` 
interface to push `a` instances into your `Slice!b`.

If that's a bit too far of a mental leap to make, you can 
certainly find a more thorough illustration of the concepts at 
play here by looking up covariance vs. contravariance in 
containers/generics online.

  — David


More information about the Digitalmars-d-learn mailing list