标签:err span += require style type 就是 mis mat
1.计算1到4的和
1 def main(args: Array[String]): Unit = { 2 3 val total = sum(1,2,3,4) 4 println(total) 5 6 } 7 8 def sum(args: Int*) = { 9 var result = 0 10 for (arg <- args) result += arg 11 result 12 }
2.使用:_*
def main(args: Array[String]): Unit = { val total = sum(1 to 5) println(total) } def sum(args: Int*) = { var result = 0 for (arg <- args) result += arg result } 报错是: Error:(4, 26) type mismatch; found : scala.collection.immutable.Range.Inclusive required: Int val total = sum(1 to 5)
在上述代码中,可以使用:_*
1 def main(args: Array[String]): Unit = { 2 3 val total = sum(1 to 4:_*) 4 println(total) 5 6 7 } 8 //变长参数 9 def sum(args: Int*) = { 10 var result = 0 11 for (arg <- args) result += arg 12 result 13 }
3. :_*作为一个整体,告诉编译器你希望将某个参数当作参数序列处理!例如val s = sum(1 to 4:_*)就是将1 to 5当作参数序列处理。
标签:err span += require style type 就是 mis mat
原文地址:https://www.cnblogs.com/shichunlei/p/12928375.html