Re: [Java Spec Report] co-variant return types

Neal Gafter <[email protected]> Fri, 18 Jun 2004 07:33:16 -0700
Newsgroups gmane.comp.java.spec-report
Message-ID <[email protected]>
Timothy Hanson wrote:
> What does the current spec say about the requirements for co-variant return types? The version I have says "This specification requires that the result type of a method is a subtype of the result types of all methods it overrides."
> 
> But javac accepts both of the following overrides:
> 
> --------------------------------------------
> public class Test extends Base {
>     Collection getC() { return null; }
>     String[] getO() { return null; }
> }
> 
> class Base {
>     Collection<String> getC() { return null; }
>     Object[] getO() { return null; }
> }
> --------------------------------------------
> 
> Has the spec changed to assignment convertible for reference types?

Unchecked conversions are allowed in covariant returns, with an appropriate 
warning required from the compiler.  This decision hasn't yet been folded into 
the spec, but is recorded as part of 4965756.  The spec text will be something 
like this:

JLS 8.4.7.3 Requirements in Overriding and Hiding

Let W = { (S, T) | (S, R) in <:u and (R, T) in <:} + <: + <:u; that is, W is
the subtyping relation augmented with unchecked reference conversion.
If a method declaration d1 with return type R1 overrides or hides the
declaration of another method d2 with return type R2, then:

If R1 is a primitive type, then it is a compile-time error if R2 is not
identical to R1.

Otherwise, if R1 is a reference type then a compile-time
error occurs if the W(R1, R2) is not true, or if one of the methods has a
return type and the other is void.



------------------------ Yahoo! Groups Sponsor --------------------~--> 
Yahoo! Domains - Claim yours for only $14.70
http://us.click.yahoo.com/Z1wmxD/DREIAA/yQLSAA/5cFolB/TM
--------------------------------------------------------------------~-> 

To unsubscribe from this mailing list, send an email to:
[email protected]
 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/java-spec-report/

<*> To unsubscribe from this group, send an email to:
     [email protected]

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/