我点了一些scala的语法,我真的不明白
object Board {
def getObjectAt(x:Int, y:Int):Placeable = return locations(x)(y)
}
Run Code Online (Sandbox Code Playgroud)
工作良好.但
object Board {
def getObjectAt(x:Int, y:Int):Placeable {
return locations(x)(y)
}
}
Run Code Online (Sandbox Code Playgroud)
返回错误
Board.scala:8: error: illegal start of declaration
return locations(x)(y)
Run Code Online (Sandbox Code Playgroud)
我发现一些东西说第二种形式说服scala编译器你试图指定一个扩展到返回类型Placeable.有没有办法解决这个问题,或者我应该避免在这里指定一个返回类型?
Dar*_*rio 10
它只是关于函数语法.
如果您的函数具有返回值,您将始终将其定义为等式(使用=),即使计算块如下:
object Board {
def getObjectAt(x:Int, y:Int):Placeable = {
return locations(x)(y)
}
}
Run Code Online (Sandbox Code Playgroud)
符号
def func(...) { ...
Run Code Online (Sandbox Code Playgroud)
是return-type的简写Unit,即没有返回值的函数.