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