marcinmoskala
06/11/2024, 9:40 AMCies
06/11/2024, 9:54 AMsealed class GuestListResult<T> {
enum class GuestListErrorType {
GuestAlreadyCheckedIn,
GuestListExceedingCapacity
}
data class GuestListSuccess<T>(val data: T) : GuestListResult<T>()
data class GuestListError<T>(val errorType: GuestListErrorType, val errorMessage: String) : GuestListResult<T>()
// ...
}
This models the reality very closely. In case of success there is no error (and thus no error type and no error message) and vice versa: when there's an error there is not success data of type T. This how it's done in Haskell and Elm: there it works really well.
Having "ad hoc" union types could work too of course. And I'm not so deep into compile design that I know why working with sum types is so hard to optimize. But saying that the second type in in the union (after |
) is the error, and the syntactical sugar for it (with !.
and !:
) is a little too much for me. I prefer generally re-usable syntax, that does not force me to only use this for errors.marcinmoskala
06/11/2024, 10:32 AMResult
. To me, the relation between union types with errors and Result
is the same as between nullable types and Optional
. It is a good argument that libraries and our practices are not adjusted, but this is backwards-compatible change, and consistent with Kotlin philosophy in general - unchecked (unexpected) exceptions should be thrown, checked (expected) should be expressed in result type. There only remains decision how we express them: with union type, with Result
, or with Either
?Cies
06/11/2024, 11:17 AMRemoteData
in our Elm projects. It's similar to Result
but has two additional variants: not-asked and loading. Using this greatly cleaned up our code and make it more hackable and defensive. http://blog.jenkster.com/2016/06/how-elm-slays-a-ui-antipattern.html
What I argue it that we need proper sum types. We need them as much as we need product types. We're just not accustomed to using them as they are not easily available in most popular languages and datastores.marcinmoskala
06/11/2024, 12:09 PMLarry Garfield
06/11/2024, 1:54 PMLarry Garfield
06/11/2024, 2:00 PMmarcinmoskala
06/11/2024, 2:14 PMLarry Garfield
06/11/2024, 2:18 PMLarry Garfield
06/11/2024, 2:19 PMCies
06/11/2024, 2:51 PM!.
and !:
then it kind of is.Larry Garfield
06/11/2024, 3:10 PMmarcinmoskala
06/11/2024, 4:05 PMLarry Garfield
06/11/2024, 5:01 PMandries.fc
06/12/2024, 5:23 AMCies
06/12/2024, 8:13 AMmikehearn
06/12/2024, 9:03 AMCies
06/12/2024, 11:35 AMLarry Garfield
06/12/2024, 12:57 PMmikehearn
06/12/2024, 1:05 PMStephan Schrƶder
06/12/2024, 1:08 PMCies
06/12/2024, 1:13 PMCies
06/12/2024, 1:15 PMLarry Garfield
06/12/2024, 1:28 PMDavid Kubecka
06/13/2024, 7:57 AMLarry Garfield
06/13/2024, 1:17 PMLarry Garfield
06/13/2024, 1:18 PMmarcinmoskala
06/14/2024, 8:59 AMCies
06/14/2024, 11:08 AM!.
and !:
I think "naked Result" is more true to what it is... (only the stdlib Result
in Kotlin has an Exception class constraint on the error parameter, where "naked Results" do not).Larry Garfield
06/14/2024, 1:41 PMLarry Garfield
06/14/2024, 1:44 PM