以下代码段在'fields.size'处失败:"值大小不是Any的成员".为什么"字段"不是"列表"类型(正如我所料)?
val condition = "a"
val fields =
if (condition == "a") {
List(1,2)
} else if (condition == "b") {
List(1,2,3)
} else if (condition == "c") {
List(1,2,3,4)
}
var g = 1
while (g < fields.size) {
//do something
g = g + 1
}
Run Code Online (Sandbox Code Playgroud)
好吧,即使"a"的if条件始终为true,也需要默认值
我会建议
val fields = condition match {
case "a" => List(1,2)
case "b" => List(1,2,3)
case "c" => List(1,2,3,4)
case _ => List.empty[Int]
}
Run Code Online (Sandbox Code Playgroud)
如果需要,您甚至可以定义fields额外安全性的类型
| 归档时间: |
|
| 查看次数: |
149 次 |
| 最近记录: |