Covariant problem

Zarathustra adam.chrapkowski at gmail.com
Sat Jul 26 15:02:12 PDT 2008


Sivo Schilling Wrote:

> Zarathustra Wrote:
> 
> > Koroskin Denis Wrote:
> > 
> > > On Thu, 24 Jul 2008 17:23:44 +0400, Zarathustra  
> > > <adam.chrapkowski at gmail.com> wrote:
> > > 
> > > > Sorry, for the amount of posts, but below is full and most easy example  
> > > > program which ilustrates problem ;)
> > > > ________________________________________________________
> > > > // class CFoo
> > > > //_______________________________________________
> > > > class CFoo{
> > > >   //_____________________________________________
> > > >   //  ::>> Attributes <<::
> > > >   //
> > > >
> > > >   //_____________________________________________
> > > >   //  << fields >>
> > > >  public real [] test(){ return [3.4, 4.5]; }
> > > > }
> > > >
> > > > // class CBar
> > > > //_________________________________________________
> > > > class CBar : CFoo{
> > > >     //_____________________________________________
> > > >   //  ::>> Attributes <<::
> > > >   //
> > > >
> > > >   //_____________________________________________
> > > >   //  << fields >>
> > > >   public uint [] test(){ return [9, 8, 7]; }
> > > > }
> > > >
> > > > void
> > > > main(char [][] args){
> > > > }
> > > > ________________________________________________________
> > > >
> > > 
> > > 
> > > I'm sorry, but your example is incorrect. You can't cast uint[] to real[].  
> > > If you need that, write a function for the task. Compiler won't do this  
> > > automatically. And yes, they are not covariant! :)
> > 
> > Of course ;) You have a right ;p
> > I'm blind. Now I see that I tied do something like that:
> > typeA function(typeX x){}
> > typeB function(typeX x){}
> > of cource compiler which function call ;p sorry my very very stiupid mistake. 
> 
> If you consider functions, e.g. not class members you're right, but this
> is not your point. You can achive your task if you make the test() member
> in CFoo final. That means CFoo.test is no longer virtual and therefore it is not necessary to have a covariant return type for CBar.test.
> Your example above with the main function containing a simple test
> scenario compiles and runs with both DMD 1.033 and DMD 2.017 quite well:
> 
> ---
> import std.stdio;
> 
> // class CFoo
> //_______________________________________________
> class CFoo{
>   //_____________________________________________
>   //  ::>> Attributes <<::
>   //
> 
>   //_____________________________________________
>   //  << fields >>
>   // public real [] test(){ return [3.4, 4.5]; }
>   final public real [] test(){ return [3.4, 4.5]; }
> }
> 
> // class CBar
> //_________________________________________________
> class CBar : CFoo{
>     //_____________________________________________
>   //  ::>> Attributes <<::
>   //
> 
>   //_____________________________________________
>   //  << fields >>
>   public uint [] test(){ return [9, 8, 7]; }
> }
> 
> void
> main(char [][] args){
>     CFoo foo = new CFoo;
>     CBar bar = new CBar;
> 
>     writefln("foo.test() gives %s as expected.", foo.test);
>     writefln("bar.test() gives %s as expected.", bar.test);
> }
> ---
>   

Great, thanks!
It's exactly that what I need.
all regards


More information about the Digitalmars-d-learn mailing list