hey! I'm trying to write a generic function to set...
# announcements
p
hey! I'm trying to write a generic function to set a default value on a nullable property of an immutable object and I have two obstacles: - can I constraint a type so it is a data class (implements a copy constructor)? - can I address the right property in the copy constructor using the KProperty? This is what I have so far:
Copy code
fun <T: ???, R> T.setDefault(prop: KProperty1<T, R>, defaultValue: R): T =
        prop.get(this)?.let { this } ?: copy(??? = defaultValue)
P.S. maybe this I'm trying to do is stupid/not worth it, feel free to point it out
m
I assume the default can't be defined on the data class definition itself?
p
I understand, thanks, so there's no way to make this generic, right?