Search code examples
functional-programmingkotlinlist-comprehensionarrow-kt

ClassCastException when using arrow-kt's list comprehension


I am trying to use a list comprehension with a guard in Kotlin. When I run the following code, I get a ClassCastException where none seems to be relevant.

data class CharWrapper(val value: Char)

@Test
fun `isolate bug`() {
    val wrappedChars = listOf(CharWrapper('Y'), CharWrapper('K'))
    val chars = listOf('Y')

    val result: List<Pair<Char, CharWrapper>> = ListK.monadFilter().bindingFilter {
        val wrappedChar = wrappedChars.k().bind()
        val char = chars.k().bindWithFilter { it == wrappedChar.value }
        char to wrappedChar
    }.fix().list

    assertThat(result, hasItem('Y' to CharWrapper('Y')))
}

This is the stacktrace:

java.lang.ClassCastException: ArrowTest$CharWrapper cannot be cast to arrow.mtl.typeclasses.MonadFilterContinuation

at ArrowTest$isolate bug$result$1.doResume(ArrowTest.kt:20)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:54)
at arrow.typeclasses.MonadContinuation$bind$$inlined$suspendCoroutineOrReturn$lambda$1.invoke(MonadContinuations.kt:59)
at arrow.typeclasses.MonadContinuation$bind$$inlined$suspendCoroutineOrReturn$lambda$1.invoke(MonadContinuations.kt:14)

Line 20 is the one that begins val result.

How can I get this comprehension to work?

I am using Kotlin 1.2.41 and Arrow-Kt 0.7.2 on Java 1.8.0_131.


Solution

  • According to pacoito, this is a known issue with Arrow that should be resolved with Arrow 0.7.4:

    ClassCastException when using state transformer with ListK #834

    Update: This was fixed in version 0.7.3.