Covariance fix (was: Idea: Lazy upcasting)

Bill Baxter dnewsgroup at billbaxter.com
Wed Mar 28 18:53:12 PDT 2007


Marcin Kuszczak wrote:
> Hello!
> 
> Any chance to get Walter interested with this? It seems that he has head so
> full of macros and constness that so small improvements will not easily
> catch him :-)
> 
> But anyway, I will try <g>

I haven't really looked to closely at your argument, but it seems there 
may be a more general principle at work here.

It seems like what you want is a way to have 'identity' return values. 
This seems to me very related to Andrei's "Challenge #1" posted a while 
back which was basically to write a function/template that returns its 
argument exactly with all relevant type modifiers.

In your example you are seeking to do that for one special argument, the 
implicit 'this' argument.

   class Base {
           this foo(int i) { return this; }
           this other() { return this; }
   }

But maybe it would be more useful to consider a way to label any 
parameter of any function as having an identity return value.  For 
instance something like:

     identity(Bar) foo(Bar x, int i) {
         return x;  // returns with the exact type of x
                    // that was passed in which could be a
                    // subclass of Bar
     }

In Luigi (http://www.dsource.org/projects/luigi) I have a templated 
method that I use for that purpose.  It returns it's argument exactly, 
which allows one to do things like:

     Button b = parent.add(new Button(), add_option1, add_option2);

The 'add' method returns the exact type of its argument.

I think the concept is interesting, but my gut tells me that if you want 
something like that it's got to be done as a template.  If you don't 
like the way the template works, then your proposal should be to change 
how templates work, not to add some new 'this' return value syntax.

--bb



More information about the Digitalmars-d mailing list