On Saturday, 23 March 2024 at 09:08:45 UTC, Per Nordlöw wrote:
Is there a reason why
```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
}
```
fails as
[1]: cannot implicitly convert expression `ds` of type
`Derived[]` to `Base[]`
[2]: cast from `Derived[]` to `Base[]` not allowed in safe code
?
The first and second is unsound (infamously allowed in Java).
Once you cast the slice you can populate it with Derived2 objects
that are not Derived, hence breaking type safety of the ds slice.
—
Dmitry Olshansky
CEO @ Glow labs
https://olshansky.me