Limits of implicit conversion of class arrays

Per Nordlöw per.nordlow at gmail.com
Sat Mar 23 09:08:45 UTC 2024


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

?


More information about the Digitalmars-d-learn mailing list