我正试图与 Kotlin 的一名后卫一起使用列表理解功能。当我运行以下代码时,我得到了一个ClassCastException,其中似乎没有任何相关性。

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')))
}

这是堆栈跟踪:
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)

第20行是开始val result的那一行。

我该如何理解?

我在Java 1.8.0_131上使用Kotlin 1.2.41和Arrow-Kt 0.7.2。

最佳答案

根据pacoito,这是Arrow的已知问题,应使用Arrow 0.7.4解决:

ClassCastException when using state transformer with ListK #834

更新:此问题已在版本0.7.3中修复。

07-28 13:08