-
Notifications
You must be signed in to change notification settings - Fork 127
Error with property in interface implemented by a record #699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This one's a bit tough. First off, properties already support records where you reference record components using property syntax: public record TestRecord(String foo) {}
TestRecord test = new TestRecord("hi");
String foo = test.foo; // access as properties That might make it seem like record access methods should implement properties too, but as you've discovered, they don't. The trouble is, record methods can't directly override getter methods. To make this work, Manifold would have to step in and generate getter methods that forward to record methods. Early on I avoided that because at the time it felt too invasive. Now that I'm revisiting the idea, I feel less opposed to it. Still, you can always write the getters manually when you need to: public interface MyInterface {
@val String foo;
}
public record TestRecord(String foo) implements MyInterface {
public String getFoo() {
return foo();
}
} Hope that helps! I'll give some more thought to auto-generating the forwarding getters too. |
Thank you for the explanation and suggestion. I find it a bit counterintuitive that implementing an interface with both a record and a regular class requires different code. I hadn't considered your proposed workaround and had reverted to using a standard getter, but your solution is definitely more effective. While it may not be intuitive, your workaround allows me to utilize properties, which I appreciate. Generating the getters automatically seems to be a good way to handle the problem. |
Uh oh!
There was an error while loading. Please reload this page.
When defining a property in an interface using the
@val
syntax, implementing that interface with a record fails due to changed getter names for records.Changing
MyInterface
to use a standard getter method resolves the issue, but it means I can no longer access the property directly and must instead use the getter:The text was updated successfully, but these errors were encountered: