斯卡拉未指定类型
问题描述:
我有以下的spark代码片段。斯卡拉未指定类型
但得到以下错误:
其中发生在这里:
val index:Int= col1(i) ; tokened +=splitted(index) + " " ; } }
^
我不能工作了它从它好像我指定所有属性的步骤。我还需要返回字符串这样的方法是string =>字符串(目前它的字符串 - >单元)这是我第一次在斯卡拉曾经编码所以道歉,如果这是一个愚蠢的问题
line => { var col1:Array[Int] = Array(1,2) ; var tokened:String = "" ;
var splitted:Array[String]=line.split(" ") ;
for (i<- 0 to col1.length) {
val index:Int= col1(i);
tokened +=splitted(index) + " " ;
}
}
答
我想这是你所需要的:
(line: String) => { /* Previously missing type annotation */
var col1:Array[Int] = Array(1,2)
var tokened:String = ""
var splitted:Array[String]=line.split(" ")
for (i<- 0 to col1.length) {
val index:Int= col1(i)
tokened += splitted(index) + " "
}
tokened /* Return value */
}
为了使函数类型明确的,你可以存储的匿名函数的函数类型的变量:
val f: (String => String) = line => { ... }
给定的行数是相当无用的。请在注明错误发生位置的代码中添加注释。 – 2013-05-06 11:53:59