首页 > 解决方案 > 为什么我的 scala 代码在 IntelliJ IDEA 中工作但在 cmd 中报告错误?

问题描述

Scala新手。我想验证我的配置文件是否正确,所以我编写了一个helloword程序。

    object HelloWorld{
      def main(args : Array[String]){
      println("HelloWorld")
      }
    }

当它在 cmd 中“scalac”文件时说warning: 1 deprecation (since 2.13.0); re-run with -deprecation for details 1 warning 但在 IDEA 中,代码有效。怎么了?

IDEA 和 cmd 中的代码

标签: scala

解决方案


如果您要添加build.sbt以下行:

ThisBuild / scalacOptions ++= Seq("-deprecation", "-Xfatal-warnings")

当出现此类弃用警告时,它将导致您的编译失败。这始终是一个好习惯。如果添加它,您将收到以下编译错误:

[error] procedure syntax is deprecated: instead, add `: Unit =` to explicitly declare `main`'s return type
[error]   def main(args : Array[String]){

然后,在应用建议时,并添加返回类型,例如:

object HelloWorld {
  def main(args : Array[String]): Unit = {
    println("HelloWorld")
  }
}

您没有收到任何警告并按HelloWorld预期打印。


推荐阅读