Jump to page: 1 2
Thread overview
Covariant problem
Jul 23, 2008
Zarathustra
Jul 23, 2008
Lars Ivar Igesund
Jul 23, 2008
Koroskin Denis
Jul 23, 2008
Lars Ivar Igesund
Jul 23, 2008
Koroskin Denis
Jul 23, 2008
Zarathustra
Jul 24, 2008
Zarathustra
Jul 24, 2008
Koroskin Denis
Jul 24, 2008
Zarathustra
Jul 24, 2008
Zarathustra
Jul 24, 2008
Zarathustra
Jul 24, 2008
Koroskin Denis
Jul 25, 2008
Zarathustra
Jul 26, 2008
Sivo Schilling
Jul 26, 2008
Zarathustra
July 23, 2008
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?
July 23, 2008
"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


July 23, 2008
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.

-- 
Lars Ivar Igesund
blog at http://larsivi.net
DSource, #d.tango & #D: larsivi
Dancing the Tango
July 23, 2008
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
> 
> 

Ok, thanks. I understand it now.
CPoint2d class is inherited by CPoint3d. Both classes have same named functions with this difference that one return CPoint2d object and second CPoin3d object. Is possible to extort to call correct function (not from parent class). It's strange, because CPoint2d can be casted to CPoint3d but not inversely.
July 23, 2008
"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.

-Steve


July 23, 2008
"Zarathustra" 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
>>
>>
>
> Ok, thanks. I understand it now.
> CPoint2d class is inherited by CPoint3d. Both classes have same named
> functions with this difference that one return CPoint2d object and second
> CPoin3d object. Is possible to extort to call correct function (not from
> parent class). It's strange, because CPoint2d can be casted to CPoint3d
> but not inversely.

I'm not quite sure how to interpret your statement.  It might be better to understand by showing some stub code.

Fill in where you see ???:

class CPoint2d : ???
{
}

class CPoint3d : ???
{
}

class CPolygon2d : ???
{
  ???[] getPoints() {}
}

class CPolygon3d : ???
{
  ???[] getPoints() {}
}

-Steve


July 23, 2008
On Wed, 23 Jul 2008 21:37:22 +0400, Steven Schveighoffer <schveiguy@yahoo.com> 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.
>
> -Steve
>
>

It should be castable to *const(U)[]* but not to U[], it's a bug otherwise:

class U
{
}

class T : U
{
   void foo() {}
}

void main() {
    T[] t = new T[42];
    U[] u = t;
    u[0] = new U;
    t[0].foo();   // access violation
}

const(U)[] would prevent that.
July 23, 2008
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[].

-- 
Lars Ivar Igesund
blog at http://larsivi.net
DSource, #d.tango & #D: larsivi
Dancing the Tango
July 23, 2008
On Wed, 23 Jul 2008 21:54:14 +0400, Lars Ivar Igesund <larsivar@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.
July 24, 2008
Steven Schveighoffer Wrote:

> 
> "Zarathustra" 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
> >>
> >>
> >
> > Ok, thanks. I understand it now.
> > CPoint2d class is inherited by CPoint3d. Both classes have same named
> > functions with this difference that one return CPoint2d object and second
> > CPoin3d object. Is possible to extort to call correct function (not from
> > parent class). It's strange, because CPoint2d can be casted to CPoint3d
> > but not inversely.
> 
> I'm not quite sure how to interpret your statement.  It might be better to understand by showing some stub code.
> 
> Fill in where you see ???:
> 
> class CPoint2d : ???
> {
> }
> 
> class CPoint3d : ???
> {
> }
> 
> class CPolygon2d : ???
> {
>   ???[] getPoints() {}
> }
> 
> class CPolygon3d : ???
> {
>   ???[] getPoints() {}
> }
> 
> -Steve
> 
> 

class CPoint3d{
}

class CPoint2d : CPoint3d{
}

class CPolygon3d{
  CPoint3d [] getPoints() {}
}
class CPolygon2d : CPolygon3d{
  CPoint2d [] getPoints() {}
}
« First   ‹ Prev
1 2