Jörg Winter
12/07/2020, 5:21 PMfun f1() = Either.right("Ok")
fun f2(message: String): Either<String, String> {
return if (message == "Ok")
Either.right("totally Ok")
else
Either.left("Not Ok")
}
suspend fun main() {
either {
val r1 = f1().bind()
val r2 = f2(r1).bind()
println(r2)
}
}
I am getting this compiler error:
File being compiled: (9,1) in /home/jw/KotlinProjects/kalidation/src/main/kotlin/test.kt
The root cause java.lang.IllegalStateException was thrown at: org.jetbrains.kotlin.codegen.state.KotlinTypeMapper$typeMappingConfiguration$1.processErrorType(KotlinTypeMapper.kt:109)
at org.jetbrains.kotlin.codegen.MemberCodegen.genSimpleMember(MemberCodegen.java:203)
at org.jetbrains.kotlin.codegen.PackagePartCodegen.generateBody(PackagePartCodegen.java:95)
at org.jetbrains.kotlin.codegen.MemberCodegen.generate(MemberCodegen.java:129)
at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generateFile(PackageCodegenImpl.java:149)
at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generate(PackageCodegenImpl.java:70)
....etc.
I don' remeber that using ".bind()" should be necessary, so what import for the monadic either-computation block shoul I be using here ?tim
12/07/2020, 6:33 PMeither.eager { ... }
?Erich Oswald
12/07/2020, 7:58 PMeither
?raulraja
12/07/2020, 9:18 PMJörg Winter
12/08/2020, 5:49 PMJörg Winter
12/08/2020, 7:38 PMeither.eager
works... and (in this case of a suspended main() I guess ?) either.invoke
work too. Thx for the pointer ! I just have to figure out the difference now.
From my earliest memories about fx-coroutines though, I did not expect there to be a special 'trigger function'.. so I am still a bit confused if I am really using fx-coroutines here.
The import is arrow.core.computations.either
btw.
My build.gradle uses this plugin
plugins {
id "org.jetbrains.kotlin.jvm" version "1.4.21"
and these dependencies
dependencies {
compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
compile("org.jetbrains.kotlin:kotlin-reflect")
compile("io.arrow-kt:arrow-fx-coroutines:$arrowVersion")
compile("io.arrow-kt:arrow-validation:$arrowVersion")
..the arrow version being 0.11.0Jörg Winter
12/08/2020, 7:43 PMJörg Winter
12/08/2020, 7:45 PMfun f1(): Either<String, String> = Either.right("Ok")
fun f2(message: String): Either<String, String> =
if (message == "Ok")
Either.right("totally Ok")
else
Either.left("Not Ok")
suspend fun main() {
val r: Either<String, String> = either.invoke {
val r1 = f1().bind()
val r2 = f2(r1).bind()
r2
}
r.fold({ err -> println("error: $err") }) { println("success: $it") }
}
Is .bind()
the way to go here btw ?raulraja
12/08/2020, 8:54 PMeither
does not depend on arrow-fx-coroutines, it depends on arrow-core. The either builder has currently a bug with suspension that @simon.vergauwen fixed and it’s still pending on a PR we have to arrow-continuations.
The error you had it’s a compiler error and should be reported because it’s unrelated to Arrow. As you said seems related to inference and how types make it to codegen after having been properly type checked.
bind
is not the way to go. Binding over monadic types like either will look like this in the either
block and invoke
will remain the only operator to apply any monadic data type such as either inside its block.
This operation removes the need to use map, flatMap and all other methods over the type that take functions when you are inside the either
block
suspend operator fun <E, A> Either<E, A>.invoke(). A
raulraja
12/08/2020, 8:54 PMJörg Winter
12/10/2020, 4:18 PMsimon.vergauwen
12/10/2020, 4:24 PMFrom my earliest memories about fx-coroutines though, I did not expect there to be a special 'trigger function'..There is no trigger function but the compiler cannot see the difference between
suspend fun either(..)
and fun either(..)
so we needed to differentiate between the two sadly.
The compiler bug you encountered was due to type arguments missing. Sometimes IDEA will show them as redundant but it will still fail at compile time 😞
We're working on updating those docs asap. It's been a year with a lot of ups and downs.. 😅