Limits of implicit conversion of class arrays
Per Nordlöw
per.nordlow at gmail.com
Mon Mar 25 07:16:35 UTC 2024
On Saturday, 23 March 2024 at 11:04:04 UTC, Dmitry Olshansky
wrote:
> The first and second is unsound (infamously allowed in Java).
In the general case, yes. But, do you see any errors with the code
```d
class Base {}
class Derived : Base {}
@safe pure nothrow unittest {
Base b;
Derived d;
b = d; // pass
Base[] bs;
Derived[] ds;
bs ~= ds; // pass
bs = ds; // fail [1], should pass
bs = cast(Base[])ds; // fail [2], should pass
}
```
> Once you cast the slice you can populate it with Derived2
> objects that are not Derived, hence breaking type safety of the
> ds slice.
Again, in the general case, yes.
So what is different in this code example compared to the general
case? Hint: this has overlaps with a missing compiler
optimization in dmd (and many other statically typed languages)
enabled by a specific kind of data flow analysis. Which one?
More information about the Digitalmars-d-learn
mailing list