Covariant problem
Koroskin Denis
2korden at gmail.com
Wed Jul 23 11:02:18 PDT 2008
On Wed, 23 Jul 2008 21:54:14 +0400, Lars Ivar Igesund
<larsivar at igesund.net> wrote:
> Steven Schveighoffer wrote:
>
>> "Lars Ivar Igesund" wrote
>>> Steven Schveighoffer wrote:
>>>
>>>> "Zarathustra" wrote
>>>>> error:
>>>>> function basis.CPolygon2d.getPoints of type CPoint2d[]() overrides
>>>>> but
>>>>> is
>>>>> not covariant with basis.CPolygon3d.getPoints of type CPoint3d[]()
>>>>>
>>>>> I don't know how to fix this problem. What does 'covariant' mean?
>>>>
>>>> Covariant means that the override returns something that can be
>>>> implicitly
>>>> casted to the base version.
>>>>
>>>> So the error looks like the base class function getPoints returns type
>>>> CPoint3d[], but the derived class is returning CPoint2d[]. In order
>>>> for
>>>> this to work, CPoint2d[] must be implicitly castable to CPoint3d[],
>>>> which probably isn't true.
>>>>
>>>> In order to diagnose the problem exactly, we would have to see code.
>>>> At
>>>> least the class hierarchy.
>>>>
>>>> -Steve
>>>
>>> Arrays are not implicitly castable (not even explicitly afaik) to
>>> another
>>> type, no matter how they are related.
>>
>> From http://www.digitalmars.com/d/1.0/arrays.html
>>
>> A dynamic array T[] can be implicitly converted to one of the following:
>>
>> a.. U[]
>> b.. void[]
>> Where U is a base class of T.
>
> Hmm, was it always that way? In any case, implicitly casting from U[] to
> T[]
> doesn't work, and if you do it explicitly, the result won't work (and
> IIRC
> doing foreach and cast was not an acceptable general solution), and as
> such
> T[] is not co-variant return type for U[].
>
No, whereas downcasting (obviously) doens't work, upcasting does.
Just check my example that I posted earlier in this thread.
More information about the Digitalmars-d-learn
mailing list