Re: Covariant Return Types

Daniel Bonniot <[email protected]> Tue, 24 Feb 2004 17:08:53 +0100
Newsgroups gmane.comp.lang.nice.general
Message-ID <[email protected]>
Just adding a few comments that were not covered by Arjan's answer.


>It seems that there is a big syntactical burden (lots of keyboarding) to
>requiring overriding to be explicit:
>  
>
...

>    merge(parent, TextElement left, TextElement right)
>          overrides merge(AbstractElement parent,
>                          AbstractElement left,
>                          AbstractElement right) 
>          = new TextElement(text:left.text + right.text);
>  
>
That's not the proposed syntax. The proposed syntax is jsut to add 
'override' in front:

  override merge(parent, TextElement left, TextElement right) = ...

With this, there isn't a big syntactical burden :-)

What this does not allow compared to what you are doing is overriding a 
method with a different name. But i'm not sure that's a very clean 
concept. I would rather keep the language simple in this case, and have 
an explicit override (that can call another method if that's desired).

>Another case is specialization by value:
>    package r; // must be getting close to the end!!!
>    public class A { }
>    public class B { }
>    public <T> T fromString(String s) { throw new Error(); }
>               A fromString("a") = new A();
>               B fromString("b") = new B();
>Do you want to allow this? It would make the type of =fromString=:
>   <T> String -> T or "a" -> A or "b" -> B
>This seems to make the type system very complicated. 
>  
>
I don't think this should be allowed. If you want to override with a 
covariant type, you should probably declare a class hierarchy instead of 
using string constants.

(BTW, this example is not covariant so it's unsafe. First fromString 
says: I can return you anything you want (if I return!). Then the 
implementation does not return anything the client wants, it returns an A.)

Daniel



-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now.
Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click