Having a bit of a headache with trying to mix Interfaces, Java Records, and Kotlin Data Classes.
Given an interface definition of
public interface JavaInterface {
String aField();
}
We can create a record that implements it:
public record JavaImplementation(String aField) implements JavaInterface {}
In the interface I named the method
aField()
to match the method generated by the record. As long as we're just in Java it's fine but it seems really awkward to have a Kotlin data class interface with it. The best I've come up with is:
data class KotlinImplementation(
val aField: String
) : JavaInterface {
override fun aField() = aField
}
(The problem exists the other way round too. If my interface is in Kotlin with
aField
as the property then data classes become simple but records don't play nice because the Kotlin interface demands they implement
getAField()
which the record doesn't generate)
As far as I can tell the fundamental issue is the naming convention records use. Has anyone come across this before and what's the most elegant way to get them to play nice with each other?
Also the interface itself must be in Java.