Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin data class with interface boilerplate

Data classes in Kotlin are great for eliminating much of the boilerplate of a Java pojo; but when a data class mirrors an interface, the two declarations still appear redundant. Take this example.

interface MyInterface {
    val foo: String
    val bar: String
    val baz: String
    val qux: String
}

data class MyImplementation(
    override val foo: String,
    override val bar: String,
    override val baz: String,
    override val qux: String
) : MyInterface

Is there any shorthand to eliminate this code duplication, i.e. to tell Kotlin that each val in the interface should be implemented by an identical val in the data class?

like image 652
jaco0646 Avatar asked Oct 27 '25 00:10

jaco0646


1 Answers

All properties in interfaces are abstract by default, so they have to be implemented (declared) by inheritors. According to this links, this feature was not planned in 2015, and it looks like it's still not implemented.

like image 186
Ilya E Avatar answered Oct 29 '25 11:10

Ilya E



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!