George
11/30/2022, 2:44 PMChris Lee
11/30/2022, 2:45 PMGeorge
11/30/2022, 2:47 PMChris Lee
11/30/2022, 2:48 PMGeorge
11/30/2022, 2:49 PMJoffrey
11/30/2022, 3:35 PMChris Lee
11/30/2022, 3:36 PMJoffrey
11/30/2022, 4:42 PMChris Lee
11/30/2022, 4:43 PMJoffrey
11/30/2022, 4:44 PMChris Lee
11/30/2022, 4:45 PMJoffrey
11/30/2022, 4:47 PMEither
, as opposed to writing your own. So in general I would expect people to add it when they really want to switch paradigmsChris Lee
11/30/2022, 4:50 PMJoffrey
11/30/2022, 5:01 PMChris Lee
11/30/2022, 5:03 PMJoffrey
11/30/2022, 5:03 PMas you look at transforming exceptions into resultsThat's what I mean by changing paradigm 🙂
Chris Lee
11/30/2022, 5:04 PMRohde Fischer
12/01/2022, 8:12 AMEither
is quite superior. First off one get map
, mapLeft
and flatMap
, which by themselves are quite powerful and useful constructs
then there's also the either-continuation (requires it to be a coroutine though) that is also quite useful for giving the code a more natural flow:
suspend fun foo(): Either<A, B> = either {
val someEither = someEither() // Either<A, B>
val a = someEither.bind() // B if someEither is right, otherwise it stops computation and returns the left side
a.doSomething()
}
Lastly Either
is also a really good solution to the frustating `null`/`Optional`/whatever talk, where if one analyze them all of them is wrong! (yes, I know the community disagree with me there). All of them doesn't tell anything and are bad ways to represent nothing, because they don't communicate what is meant by nothing. having Either<DomainError, A>
and then having domain-error be something like:
sealed interface DomainError
data class NoSuchElement(val id): DomainError
object ServiceUnavailable: DomainError
// ... etc
will communicate a lot better, and be many fold more true to the spirit of DDD and my experience is it's worth (by a lot!) the additional effort because it gives an explicit understanding and clear code analysis for other developersdave08
12/05/2022, 3:50 AMdave08
12/05/2022, 3:51 AMdave08
12/05/2022, 3:52 AMRohde Fischer
12/06/2022, 8:24 AMSee how kotlin.Result uses Any, but wraps it in a value class, then you might not need any wrappers...... using
Any
like that is kind of a big turn down if you ask me, especially in a strong type system that allows stronger types. Using Any
there would be a major design mistake... fortunately Jetbrains did not do that mistake, kotlin.Result
does not! use Any
but generics: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/
that said, it looks like a slightly weird design, a bit like a less useful/powerful interpretation of the Either
monad that tries to encompass the common design mistake of using exceptions for domain logic. We do have the language power to express a proper distinction between domain errors end system/programming errors. Let's keep exceptions exceptional and only use them when things are truely wrong and design our domain errors - which are expected - instead :)dave08
12/06/2022, 10:29 AMRohde Fischer
12/06/2022, 1:27 PMRohde Fischer
12/06/2022, 1:30 PMdave08
12/06/2022, 1:30 PMdave08
12/06/2022, 1:31 PMdave08
12/06/2022, 1:32 PMRohde Fischer
12/06/2022, 1:39 PMdave08
12/06/2022, 1:42 PM