Introducing Nullable Reference Types in C#. Is there hope for D, too?
Timon Gehr
timon.gehr at gmx.ch
Tue Nov 21 21:49:44 UTC 2017
On 21.11.2017 07:46, Dmitry Olshansky wrote:
>
> The spec describes unsound language, the hole in type-system are plugged
> at VM level by run-time checks.
>
> Also this jawel:
>
> Cat[] cats = new Cat[3];
> ...
> Animals[] animals = cats; // the same array
>
> animals[0] = new Dog();
>
> cats[0].smth(); // ClassCast exception or some such
>
Actually, the "java.lang.ArrayStoreException" will be thrown already
when you attempt to add the dog to the cat array. This is by design
though (and explicitly supported by the JVM). The reason why the
null-related Java type system hole does not lead to memory corruption is
that the JVM does not support generics. (It's all translated to explicit
type casts that are expected to always succeed, but the JVM still checks
them.)
More information about the Digitalmars-d
mailing list