[records] customized accessor returning subtype

Joe Darcy joe.darcy at oracle.com
Tue Dec 10 00:04:09 UTC 2019


FYI, this issue was raised in the CSR review:

> In "An implicitly declared public accessor method with the same name 
> as the record component, whose return type is the declared type of the 
> record component, unless a public method with the same signature is 
> explicitly declared in the body of the declaration of R."
>
> does that imply a "covariant override" accessor can be explicitly 
> defined as the signature does not include the return type? For example 
> if the component had a type of Object and the user defined an 
> accesssor method with a type of String? Would a bridge method need to 
> be defined? Should the return type be constrained to match too?
>

https://bugs.openjdk.java.net/browse/JDK-8233433?focusedCommentId=14302123&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-14302123

and replied to

> This is a very good question. I've actually changed the description of 
> accessor methods and constructor methods to make this clearer (I 
> hope). In essence there is a two stage process. For accessor methods: 
> If you have the name of a component and an empty formal parameter 
> list, then you are considered to be an accessor method for the 
> component. We then ask additional criteria of you - it's a 
> compile-time error if you don't satisfy them! This includes having a 
> return type that is identical to the derived type of the record 
> component, being |public|, etc. It's essentially the same for 
> constructors: a record component list essentially derives a canonical 
> constructor signature. If you are a constructor that is 
> override-equivalent, then you are a canonical constructor. We then ask 
> additional criteria of you, being |public|, not being generic, etc.
>
> These definitions clear up a lot of corner cases, and more importantly 
> lead to better error messages. (In your particular example, the 
> |String| returning method would /not/ be an accessor for the |Object| 
> component, so one would be implicitly declared for you, and then you'd 
> get an error as we'd have two overloads with identical signatures).
>
https://bugs.openjdk.java.net/browse/JDK-8233433?focusedCommentId=14302457&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-14302457

Cheers,

-Joe

On 12/9/2019 11:09 AM, Dmitry Bessonov wrote:
> Is there any reason for not allowing a customized accessor to return rc subtype:
>
> record R(Number number) {
>      public Integer number() {
>          return 42;
>      }
> }
>
> Error: java: invalid accessor method in record R
>    (return type of accessor method number() is not compatible with type of record component number)
>
>
> dmitry
>


More information about the amber-dev mailing list