Limits of implicit conversion of class arrays
Per Nordlöw
per.nordlow at gmail.com
Sun Mar 31 21:04:55 UTC 2024
On Thursday, 28 March 2024 at 01:53:52 UTC, Steven Schveighoffer
wrote:
>> ```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
>> }
>> ```
>
> Yes, it's unsafe, as you can replace an element of `ds` with
> something that has no relation to `Derived`.
This is a suggested change that when _applied_ will make the code
unsafe yes. But the code in its current form is safe and the
compiler could be extended to prove it.
More information about the Digitalmars-d-learn
mailing list