hey! is there an elegant way to do this?: I want a...
# announcements
x
hey! is there an elegant way to do this?: I want a data class that takes a value, but if the value is null, have that value be the result of an operation on other two values. Example in thread
Data Class Test( val a: Long, val b: Long, val c: Long?){ val c = if (c==null) a + b else c }
k
i think this is a good stackoverflow that covers your question. explains different approaches and the whys https://stackoverflow.com/a/46376746
x
thank you 🙂
i
Copy code
data class Test(
    val a: Long,
    val b: Long,
    val c: Long
) {
    constructor(a: Long, b: Long, c: Long? = a + b) : this(a, b, c ?: a + b)
}
You can add a constructor. Note that their declarations must differ edit: added default argument for c
2
👍 2