David Kubecka
02/22/2023, 2:56 PMdata class A(param: Int)
data class B(param: Int) {
fun toA() = A(param)
}
I want to make the converter work also on a nullable receiver providing some default value. I know I can do the following:
data class B(param: Int)
fun B?.toA() = this?.let { A(param) } ?: A(42)
but wonder whether there's a better option. At least if I can somehow keep the non-nullable toA
inside the class definition (keeping its name) and refer to it in fun B?.toA()
.
UPDATE: I realize now I can indeed refer to original toA
from B?.toA()
. Still it doesn't look very nice...Thiago
02/22/2023, 2:59 PMfun B?.toA(): A = A(this?.param ?: DefaultValue)
David Kubecka
02/22/2023, 3:04 PMRuckus
02/22/2023, 5:16 PMfun B?.toA() = this?.toA() ?: A(42)
doesn't look very nice? If so, I'm not sure I understand what you're asking.David Kubecka
02/22/2023, 5:49 PMI'm maybe more interested in the option of keeping the converter inside the class def.
Ruckus
02/22/2023, 6:47 PM