Re: Covariant Return Types

[email protected] Tue, 24 Feb 2004 18:56:44 -0600
Newsgroups gmane.comp.lang.nice.general
Message-ID <[email protected]>
Quoting Daniel Bonniot <[email protected]>:

> >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) = ...

I don't think this works in general, when =merge= is overloaded already. Please
see the example in my reply to Arjan that has the heading "Okay, but try this
version."

> 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).

This is not a big issue for me. I was just noting that the syntax I was using
provided for that possibility.

> >    public <T> T fromString(String s) { throw new Error(); }
> >               A fromString("a") = new A();
> >               B fromString("b") = new B();
> 
> (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.)

You always catch these. :) Try: 

    public A fromString(String s) { throw new Error(); }
           A fromString("a") = new A();
           B fromString("b") = new B();

But, I think we all agree that this is not something worth allowing.

Regards,
Brian




-------------------------------------------------------
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