Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
折叠列表与kotlin中的分解分配配对_Kotlin_Fold_Destructuring - Fatal编程技术网

折叠列表与kotlin中的分解分配配对

折叠列表与kotlin中的分解分配配对,kotlin,fold,destructuring,Kotlin,Fold,Destructuring,我尝试从对字符列表的折叠操作中获得多个结果,然后对这两个值使用解构赋值,并将其分配给各自的变量。但这似乎导致kotlin编译器出现异常: java.lang.UnsupportedOperationException: Don't know how to generate outer expression for class <closure-Test$1> 这将导致异常 val fstSec = "fst" val pair = "this is a test for fold

我尝试从对字符列表的折叠操作中获得多个结果,然后对这两个值使用解构赋值,并将其分配给各自的变量。但这似乎导致kotlin编译器出现异常:

java.lang.UnsupportedOperationException: Don't know how to generate outer expression for class <closure-Test$1>
这将导致异常

val fstSec = "fst"

val pair = "this is a test for folding to pair"
    .toCharArray()
    .fold(Pair(0, 0), { sumPair, char ->
        when (fstSec) {
            "fst" -> Pair(sumPair.first + char.toInt()*2, sumPair.second + char.toInt())
            "snd" -> Pair(sumPair.first + char.toInt(), sumPair.second + char.toInt()*2)
            else -> throw RuntimeException("exception")
        }
    })

println("( ${pair.first} , ${pair.second} )")
这一个按预期工作,我唯一删除的是解构。奇怪的是,如果我删除了内部when(并仅用一个Pair构造函数替换它),那么代码两次都可以工作


提前感谢。

这是Kotlin脚本编译中的一个错误


感谢您的报告:

这似乎是一个bug。顺便说一句,我试着用Kotlin 1.2.10编译第一个示例,效果很好。可能已经修好了。您使用的Kotlin版本是什么?Kotlin版本1.2.0(JRE 1.8.0_144-b01)我使用的是Kotlin 1.2,当我将整个内容包装到一个函数中时,它工作得很好。否则它会抱怨只有局部变量才支持解构。在编译它的时候还没有尝试过(这有什么不同吗?)
val fstSec = "fst"

val pair = "this is a test for folding to pair"
    .toCharArray()
    .fold(Pair(0, 0), { sumPair, char ->
        when (fstSec) {
            "fst" -> Pair(sumPair.first + char.toInt()*2, sumPair.second + char.toInt())
            "snd" -> Pair(sumPair.first + char.toInt(), sumPair.second + char.toInt()*2)
            else -> throw RuntimeException("exception")
        }
    })

println("( ${pair.first} , ${pair.second} )")