我是Scala的新手,我之前从未编写或编译过程序.我正在尝试简单地运行以下Hello World示例,我将其保存在文件名scalaApp.scala中
object scalaApp extends App {
def main(args: Array[String]) {
println("Hello, world!")
}
}
Run Code Online (Sandbox Code Playgroud)
当我在文件目录中的终端并键入"scalac scalaApp.scala"时,我收到以下错误消息:
scalaApp.scala:4: error: overriding method main in trait App of type (args: Array[String])Unit;
method main needs `override' modifier
def main(args: Array[String]) {
^
one error found
Run Code Online (Sandbox Code Playgroud)
我以为我已按照所有指示在我的计算机上正确安装Scala 2.10.3,但我不知道如何测试它,因为我甚至无法编译这个简单的程序.如果我在终端输入"scala",我会得到一个scala提示符,我可以在其上运行"1 + 1"之类的命令.我不确定这表明了多少.我已将SCALA_HOME变量添加到〜/ .profile,并将SCALA_HOME添加到〜/ .profile中的PATH变量中.如果有人能告诉我我做错了什么,或者给我一个关于我可能找到答案的建议,我会很感激.
Von*_*onC 18
自App
扩展DelayedInit
以来,您不应该定义main
函数
这应该足够了:
object scalaApp extends App {
println("Hello, world!")
}
Run Code Online (Sandbox Code Playgroud)
编译器为您创建此函数,并将其传递给delayedInit(x: => Unit)
方法(请注意参数中的call-by-name).
编译器将发出:
object Main extends DelayedInit {
def delayedInit(x: => Unit = { println("Hello, worl!") }) = // impl is left for us to fill in
}
Run Code Online (Sandbox Code Playgroud)