为什么在Scala中非法开始声明?

Bas*_*evs 15 scala

对于以下代码:

package FileOperations
import java.net.URL

object FileOperations {
    def processWindowsPath(p: String): String {
        "file:///" + p.replaceAll("\\", "/")
    }
}
Run Code Online (Sandbox Code Playgroud)

编译器给出错误:

> scalac FileOperations.scala
FileOperations.scala:6: error: illegal start of declaration
        "file:///" + p.replaceAll("\\", "/")
Run Code Online (Sandbox Code Playgroud)

为什么?怎么修?

Jon*_*ffe 22

您从processWindowPath方法声明中缺少一个=.

package FileOperations
import java.net.URL

object FileOperations {
    def processWindowsPath(p: String): String = {
        "file:///" + p.replaceAll("\\", "/")
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,有很多代码没有返回值。如果该方法返回一个值,则需要 = 符号。第 8 页有链接文档中的第一个示例。 (2认同)

mic*_*ebe 7

object FileOperations {
  def processWindowsPath(p: String): String  = {
    "file:///" + p.replaceAll("\\", "/")
  }
}
Run Code Online (Sandbox Code Playgroud)

有一个失踪=.Scala中的方法以这种方式定义:

def methodName(arg1: Type1, arg2: Type2): ReturnType = // Method body
Run Code Online (Sandbox Code Playgroud)