1

我正在尝试在 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。

4

1 回答 1

2

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

使用带有 ListK 的状态转换器时出现 ClassCastException #834

更新:这已在 0.7.3 版中修复。

于 2018-06-07T15:03:33.920 回答