@Override on a record component compiles but does not work
Gavin Bierman
gavin.bierman at oracle.com
Tue Jul 20 17:04:47 UTC 2021
Hi Remi,
Just to back up Vicente’s reply.
You’ll recall that the intent of the implicit accessor declaration is that these two record declarations are, in essence, identical:
record R(@Override int x){}
and
record R(int x){
@Override
public int x(){
return this.x;
}
}
So, given that assumption, and the snippet that Vicente included from JLS 16.9.6.4.4 that explains that @Override has a special meaning for accessor methods, then you get the behaviour that you have observed.
Thanks,
Gavin
On 19 Jul 2021, at 20:54, Vicente Romero <vicente.romero at oracle.com<mailto:vicente.romero at oracle.com>> wrote:
Hi Remi,
According to JLS 16 9.6.4.4:
...
The clause about a record class is due to the special meaning of @Override in a record
declaration. Namely, it can be used to specify that a method declaration is an accessor
method for a record component. Consider the following record declaration:
record Roo(int x) {
@Override
public int x() {
return Math.abs(x);
}
}
}
The use of @Override on the accessor method int x() ensures that if the record
component x is modified or removed, then the corresponding accessor method must be
modified or removed too.
...
So it is OK to annotate a record component with the @Override annotation without the need for a superinterface defining an override equivalent method to exist,
Thanks,
Vicente
On 7/19/21 12:49 PM, Remi Forax wrote:
It does not seems that @Override correctly compiles on a record but the "override" check is not done.
By example,
interface Named {
String name();
}
record Foo(@Override String name) implements Named { }
actually compiles but
record Foo(String name, @Override String bar) implements Named { }
also compiles ??
It seems that the logic that checks if a method "overrides" another (in the general sense i.e replace another method) is not implemented if @Override is declared on a record component.
regards,
Rémi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20210720/97b7f089/attachment.htm>
More information about the compiler-dev
mailing list