Reputation: 909
Hi there I have gone thru the kotlin documentation and haven't found anything. What I want to do is have a generic thar must be a data class, something like
data class MyData(val pop1:Long,val pop2:String,...)
fun class MyGenericClass<T : isDataClass>(o : T){
// This is the important part
fun useCopy(value : Long) = t.copy(pop1 = value)
}
What I really need to achieve is to be able to use the copy function of data classes in a generic way(pop1 will always be a member of my data classes)
Thanks in advance
Upvotes: 4
Views: 4976
Reputation: 81859
You should consider using an interface for that particular problem. Just delegate to the data class copy
in the implementation.
data class MyData(val pop1: Long, val pop2: String) : Pop1Data {
override fun copy(pop1: Long) {
copy(pop1 = pop)
}
}
interface Pop1Data {
fun copy(pop1: Long)
}
class MyGenericClass<T : Pop1Data>(private val o: T) {
// This is the important part
fun useCopy(value: Long) = o.copy(value)
}
Upvotes: 3