【问题标题】:ClassCastException when using arrow-kt's list comprehension使用箭头 kt 的列表理解时出现 ClassCastException
【发布时间】:2018-06-05 14:17:18
【问题描述】:

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

【问题讨论】:

    标签: functional-programming kotlin list-comprehension arrow-kt


    【解决方案1】:

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

    ClassCastException when using state transformer with ListK #834

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

    【讨论】:

    • 很好的问题和答案!
    • 我们应该尽快发货
    猜你喜欢
    • 2022-10-19
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 2017-08-23
    • 2011-03-10
    相关资源
    最近更新 更多