julian
06/03/2020, 1:31 AMsealed class A {
data class B(val b: String) : A()
data class C(val c: Int) : A()
}
class D
val a: A = TODO()
fun f(a: A): D {
when (a) {
is A.B -> { TODO() }
is A.C -> { TODO() }
}
}
I end up with so many of these `when`s throughout my code. Am I doing something wrong? Or is my allergy to it due to coming from doing only OOP for years? With OOP A
would have some abstract method that B
and C
would implement.bloder
06/03/2020, 2:23 AMbloder
06/03/2020, 2:26 AMA -> B
transformation would help to decrease its usage in some casesBob Glamm
06/03/2020, 3:19 AMwhen
is pretty consistent with mathematicians combining partial functions to yield a whole function, for example:
f(x) = { x | [[ x > 2 -> x + 2 ]]; [[x <= 2 -> -x]] }
Bob Glamm
06/03/2020, 3:19 AMwhen x > 2 then yield x + 2; otherwise when x <= -2 then yield -x
Bob Glamm
06/03/2020, 3:22 AMA
to have an abstract method that B
and C
implement, especially if a future D
or E
that extended A
had no need for such a methodpakoito
06/03/2020, 11:39 AMpakoito
06/03/2020, 11:39 AMpakoito
06/03/2020, 11:40 AMpakoito
06/03/2020, 11:40 AMpakoito
06/03/2020, 11:40 AMjulian
06/03/2020, 4:05 PMjulian
06/03/2020, 4:13 PM... maybe typeclasses that provides aRoughly speaking, how would I go about this? I know what typeclasses are in the abstract. And I'm familiar with the common ones in Arrow (e.g. applicative, functor, monad). And I know how Arrow can take care of boilerplate code-gen for custom typeclasses. But identifying and appropriately defining a custom typeclass is new territory for me.transformation ...A -> B
bloder
06/03/2020, 5:31 PMfun x (a: Result<T>): Result<F>
, people there were transforming T
in F
by getting T
value and applying a pattern mathing in it then transforming in F
just to return a Result<F>
instead of just map it using Result
monad api that already had a pattern matching for error use cases. I think it´s good to take a look in your code and places that ure applying pattern matching and try to figure out how typeclasses can help (maybe some times foldables can help maybe other times just simple functors can help like this case of my last company).bloder
06/03/2020, 5:53 PMjulian
06/03/2020, 7:20 PM