jean
03/17/2023, 11:26 AMdata class SomeData(val someValue: String) {
companion object {
fun someFunction() {}
}
}
// OR
data class SomeData(val someValue: String) {
companion object
}
fun SomeData.Companion.someFunction() {}
simon.vergauwen
03/17/2023, 12:13 PMcompanion
itself when possible. I would say follow rule of thumb - simplest solution.
It doesn't require an import that way (, and is better for IDEA).jean
03/17/2023, 12:25 PMsimon.vergauwen
03/17/2023, 12:29 PMsince it could hold internal variable that could turn your code in a non-pure way over timeI guess I'm more in the camp of "this should be caught at PR review time". But even then, this doesn't suffer from that problem since
companion object
is "static" so it doesn't have access to any "mutable state" that the class could potentially hold.
I am also in favor of adding fun toOther(): OtherDataClass
inside SomeData
rather than an extension function for the same rationale of "not needing an import".
Simple rule of thumb applies here: data class
should only have `val`'s in the constructor, or pure functions inside the body or companion object
.simon.vergauwen
03/17/2023, 12:30 PMsuspend
is a functional concept
⢠KotlinX Coroutines is an FP concurrency library if you ask me. It follows all the same principles as IO
of other FP languages.
⢠Collections.kt
exists out of FP combinators.
⢠Nullable types are derived from Option
, with some slight differences and trade-offs (nested nullable type problem only being problematic in generic code).