为什么在 Scala 中非法开始声明?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3956670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:30:47 来源:igfitidea点击:
Why illegal start of declaration in Scala?
提问by Basilevs
For the following code:
对于以下代码:
package FileOperations
import java.net.URL
object FileOperations {
def processWindowsPath(p: String): String {
"file:///" + p.replaceAll("\", "/")
}
}
Compiler gives an error:
编译器报错:
> scalac FileOperations.scala
FileOperations.scala:6: error: illegal start of declaration
"file:///" + p.replaceAll("\", "/")
Why? How to fix?
为什么?怎么修?
回答by Jon McAuliffe
You're missing an = from the processWindowPath method declaration.
您缺少 processWindowPath 方法声明中的 = 。
package FileOperations
import java.net.URL
object FileOperations {
def processWindowsPath(p: String): String = {
"file:///" + p.replaceAll("\", "/")
}
}
回答by michael.kebe
object FileOperations {
def processWindowsPath(p: String): String = {
"file:///" + p.replaceAll("\", "/")
}
}
There is a missing =. Methods in Scala are defined this way:
有一个缺失=。Scala 中的方法是这样定义的:
def methodName(arg1: Type1, arg2: Type2): ReturnType = // Method body

