Scala初心者はこちら。
これが私の単純なforループです
def forExampleStoreValues = {
println(">>forExampleStoreValues");
val retVal = for{i <- 1 to 5 if i % 2 == 0} yield i;
println("retVal=" + retVal);
}
私の期待は、これを呼び出すと、最後の値が自動的に返されることです。ただし、これをメインから呼び出すと、
object MainRunner {
def main(args: Array[String]){
println("Scala stuff!"); // println comes from Predef which definitions for anything inside a Scala compilation unit.
runForExamples();
}
def runForExamples() {
val forLE = new ForLoopExample(); // No need to declare type.
println("forExampleStoreValues=" +forLE.forExampleStoreValues)
}
}
出力は次のとおりです。
>>forExampleStoreValues
retVal=Vector(2, 4)
forExampleStoreValues=()
そのため、retval を明示的に返そうとします。
def forExampleStoreValues = {
println(">>forExampleStoreValues");
val retVal = for{i <- 1 to 5 if i % 2 == 0} yield i;
println("retVal=" + retVal);
return retval;
}
これは与える:
method forExampleStoreValues has return statement; needs result type
そこで、関数の署名を次のように変更します。
def forExampleStoreValues():Vector
与える:
Vector takes type parameters
この段階では、何を入力すればよいかわかりません。必要のないことをしていないことを確認したいと思います。