Sorry new to all of this but is there a way to wra...
# announcements
w
Sorry new to all of this but is there a way to wrap and extend a data class. Say if I had a data class
Summary
and I wanted to create a data class
SummaryCalculation
that had all the fields of Summary but had new field also.
Copy code
data class Summary(val aProperty:String, val items:List<BillableItem>)

//This I don't know
data class BillingSummary(initialSummary:Summary, totalItemsBilled:Long):Summary
It doesn't actually need to share an equals/hashcode, just expose the same api. I'm more or less trying to create an envelope that holds addition properties. that can be created from the initial object.
d
You can share a common interface between the two:
Copy code
interface Summary {
    val aProperty: String
}

data class SummaryData(override val aProperty: String) : Summary

data class BillingSummary(val initialSummary: Summary, val totalItemsBilled: Long) : Summary by initialSummary
w
Thank you I think that should work. Not familiar with the
by initalSummary
syntax.
d
It implements the interface by delegating all functions / properties to the implementation you give it.
m
@wes to understand that syntax read about delegation: https://kotlinlang.org/docs/tutorials/kotlin-for-py/inheritance.html#delegation
w
@Matteo Mirk Thanks
👍 1