为什么在Scala中非法开始声明?
对于以下代码:为什么在Scala中非法开始声明?,scala,Scala,对于以下代码: package FileOperations import java.net.URL object FileOperations { def processWindowsPath(p: String): String { "file:///" + p.replaceAll("\\", "/") } } 编译器给出一个错误: > scalac FileOperations.scala FileOperations.scala:6: erro
package FileOperations
import java.net.URL
object FileOperations {
def processWindowsPath(p: String): String {
"file:///" + p.replaceAll("\\", "/")
}
}
编译器给出一个错误:
> scalac FileOperations.scala
FileOperations.scala:6: error: illegal start of declaration
"file:///" + p.replaceAll("\\", "/")
为什么??如何修复?processWindowPath方法声明中缺少一个=
package FileOperations
import java.net.URL
object FileOperations {
def processWindowsPath(p: String): String = {
"file:///" + p.replaceAll("\\", "/")
}
}
缺少一个=
。Scala中的方法定义如下:
def methodName(arg1: Type1, arg2: Type2): ReturnType = // Method body
所有scala教程列表都缺少该符号是的,那里有很多代码没有返回值。如果该方法返回一个值,则需要=符号。第8页有链接文档中的第一个示例。这些函数是否返回值?如果不使用,则不应使用
=
。是的,只有不返回值的函数在教程中不使用=登录。与def
一起使用时,是否应该调用=
的特定名称?
def methodName(arg1: Type1, arg2: Type2): ReturnType = // Method body