Charles Flynn
05/25/2023, 9:40 AMpublic 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.Adam S
05/25/2023, 9:50 AMCharles Flynn
05/25/2023, 9:52 AMAdam S
05/25/2023, 9:53 AMAdam S
05/25/2023, 9:54 AMJavaInterface
, purely for purposes of interoping nicely
interface JavaInterfaceKt : JavaInterface {
val aField: String
fun aField(): String = aField
}
Adam S
05/25/2023, 9:56 AMCharles Flynn
05/25/2023, 11:59 AMPaul Griffith
05/25/2023, 3:27 PM@JvmRecord
on a data class should emit a proper JVM record with the same syntax for access from Java: https://kotlinlang.org/docs/jvm-records.html#declare-records-in-kotlinPaul Griffith
05/25/2023, 3:28 PMPaul Griffith
05/25/2023, 3:32 PM