No polymorphism?

Dgame via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 24 10:55:24 PDT 2017


On Monday, 24 July 2017 at 17:33:48 UTC, Adam D. Ruppe wrote:
> On Monday, 24 July 2017 at 17:29:55 UTC, Dgame wrote:
>> 	S[] ss = [new S()];
>> 	test1(ss); // Fails
>>
>> Why isn't the compiler able to deduce S[] => I[]? Or is it 
>> just me?
>
> This is exactly because of polymorphism. Consider the following:
>
> ```
> S[] ss = [new S()];
> I[] i = ss; // pass it to the function or whatever for implicit 
> conversion
>
> class OtherDerived : I {}
>
> i[0] = new OtherDerived(); // looks OK, otherDerived is also 
> interface I
> ```
>
> But now, ss[0], the same array as i, no longer points to an S! 
> You broke the type system.

So, tired it is. Thanks a lot.


More information about the Digitalmars-d-learn mailing list