Apologies in case I am missing something obvoius, ...
# arrow
j
Apologies in case I am missing something obvoius, but I was not following arrow-fx-coroutines for a while now. So using arrow-fx-coroutines 0.11.0, should this be possible with Kotlin 1.4.20 ?
Copy code
fun 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 ?
t
Isn't it
either.eager { ... }
?
e
I think I recently had the same or a similar compiler error. Does it work if you add explicit type arguments to the
either
?
r
That looks like a compiler backends bug. What does the Gradle file looks like?
j
hey, thanks for your suggestions and questions... will post my specific setup here as soon as possible !
@tim
either.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
Copy code
plugins {
    id "org.jetbrains.kotlin.jvm" version "1.4.21"
and these dependencies
Copy code
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.0
@raulraja @Erich Oswald So what I also did to get rid of the compiler error (apart from using either.eager) was fully specifying the types on both f1() and f2() guess there was a problem with type inference somewhere, backend is Kotlin 1.4.21 JVM (if that's what you asked?)
So my full snippet is this right now:
Copy code
fun 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 ?
r
@Jörg Winter
either
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
Copy code
suspend operator fun <E, A> Either<E, A>.invoke(). A
to perform that monadic bind you only need arrow-continuations : https://gist.github.com/raulraja/e732bdaace04426d3be4380b7760a8c0#file-instances-kt-L49-L55
j
To be honest, I am thoroughly confused right now.... but never mind :) When everything converges in a documented release (like right now the mouse-over example for IO on arrow-kt.io), it will probably be clearer Basically, a straightforward simple example of using 2 monadic either functions inside a fx-coroutines block would be fine.
s
From 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.. 😅
👍 2