Larry Garfield
05/14/2024, 3:22 PMCLOVIS
05/14/2024, 3:32 PMLarry Garfield
05/14/2024, 3:35 PMval test = Result.fail("Bad")
test.<my methods are not available in the IDE>
Larry Garfield
05/14/2024, 3:36 PMsealed class Result<out T, out E> {
data class Success<T>(val value: T) : Result<T, Nothing>()
data class Error<E>(val error: E) : Result<Nothing, E>()
fun <T, E> Result<T, E>.then(block: () -> T) = when(this) {
is Result.Success -> Result.of { block() }
else -> this
}
fun <T, E> Result<T, E>.error(block: () -> T) = when(this) {
is Result.Error -> Result.fail(block())
else -> this
}
companion object {
fun <T> ok(value: T): Result<T, Nothing> = Result.Success(value)
fun <E> fail(error: E): Result<Nothing, E> = Result.Error(error)
}
}
CLOVIS
05/14/2024, 3:37 PMtest
? (CTRL SHIFT P in IDEA)Larry Garfield
05/14/2024, 3:37 PMResult<Nothing, String>
CLOVIS
05/14/2024, 3:38 PMmy own result monad object, largely for educational purposesIf it's for educational purposes, I recommend naming it something else to avoid import weirdness 🙂 But yeah, in the real world please use #arrow
Larry Garfield
05/14/2024, 3:38 PMCLOVIS
05/14/2024, 3:38 PMCLOVIS
05/14/2024, 3:38 PMLarry Garfield
05/14/2024, 3:39 PMCLOVIS
05/14/2024, 3:39 PMLarry Garfield
05/14/2024, 3:42 PMCLOVIS
05/14/2024, 3:44 PMwe’re a few versions behinda few versions of what? Are you already using Arrow? Arrow 2.0 is really soon, probably a few weeks
CLOVIS
05/14/2024, 3:45 PMLarry Garfield
05/14/2024, 3:45 PMLarry Garfield
05/14/2024, 3:46 PMCLOVIS
05/14/2024, 3:46 PMhfhbd
05/14/2024, 3:46 PMCLOVIS
05/14/2024, 3:46 PMCLOVIS
05/14/2024, 3:46 PMCLOVIS
05/14/2024, 3:47 PMfun <T, E> then(block: () -> T) = when(this) {
is Result.Success -> Result.of { block() }
else -> this
}
fun <T, E> error(block: () -> T) = when(this) {
is Result.Error -> Result.fail(block())
else -> this
}
(I removed Result.
from both of them)Larry Garfield
05/14/2024, 3:47 PMLarry Garfield
05/14/2024, 3:47 PMCLOVIS
05/14/2024, 3:47 PMthen
and error
to the top-levelCLOVIS
05/14/2024, 3:48 PMCLOVIS
05/14/2024, 3:48 PMLarry Garfield
05/14/2024, 3:48 PMCLOVIS
05/14/2024, 3:49 PMLarry Garfield
05/14/2024, 3:49 PMCLOVIS
05/14/2024, 3:50 PMResult<>.
means "this takes an implicit parameter of type `Result`". But being in the brackets of the Result
class means you have to call it as somethingOfTypeResult.error
. You end up with requiring result two times which is why the compiler didn't let you call the function (you only provided once)CLOVIS
05/14/2024, 3:51 PMResult<>.
syntax) at the top-level of files. There are very useful cases for putting them inside of classes, but those are usually quite advanced so I wouldn't recommend beginners to think about the implications too muchLarry Garfield
05/14/2024, 3:52 PMCLOVIS
05/14/2024, 3:53 PMLarry Garfield
05/14/2024, 3:54 PMCLOVIS
05/14/2024, 3:55 PMLarry Garfield
05/14/2024, 3:55 PMCLOVIS
05/14/2024, 3:55 PMLarry Garfield
05/14/2024, 3:55 PMCLOVIS
05/14/2024, 3:56 PMLeft
, it's really quick to get used to itCLOVIS
05/14/2024, 3:57 PMResult<Foo, Unit>
you also have to remember than the failure is on the left when you're reviewing code in GitHub or whatever, so you already know that left is the failure caseLarry Garfield
05/14/2024, 3:58 PMResult<Success, Error>
. So error is on the right.CLOVIS
05/14/2024, 3:59 PMLarry Garfield
05/14/2024, 3:59 PMCLOVIS
05/14/2024, 3:59 PMtypealias Success = Either.Right
typealias Failure = Either.Left
CLOVIS
05/14/2024, 4:00 PMLarry Garfield
05/14/2024, 4:00 PMLarry Garfield
05/14/2024, 4:00 PMCLOVIS
05/14/2024, 4:01 PMCLOVIS
05/14/2024, 4:01 PMLarry Garfield
05/14/2024, 4:50 PM/Users/lg9549/.gradle/caches/modules-2/files-2.1/io.arrow-kt/arrow-annotations-jvm/1.2.4/f81ec8df8fe610b774a8a9100060c46f7f028347/arrow-annotations-jvm-1.2.4.jar!/META-INF/arrow-annotations.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.7.1.
I read that as “you’re on Kotlin 1.7, Arrow requires 1.9, sucks to be you”. Correct?CLOVIS
05/15/2024, 7:41 AMCLOVIS
05/15/2024, 7:43 AMwhen
expressions had to be exhaustive so you had to add a bunch of else
, but I don't remember anything in recent versions), so you should be able to upgrade just Kotlin without touching your other dependencies, if you're worried about upgrading too much stuff at once