一般情况下,我们会倾向于打开各种warning,以便规范我们的代码.不过,scala有时候有些warning也是蛮扯的.比如这么一段
$ scala -Xlint:missing-interpolator
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_05).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val key="value"; println("$key")
:12: warning: possible missing interpolator: detected interpolated identifier `$key`
println("$key")
^
$key
key: String = value
因为key是变量,它提示我们检测到了interpolated identifier,也就是那个”$”. 因为有代码是这样写的:
scala> val key="value"; println(s"$key") value key: String = value
编译器提示我们可能我们是想写成如上那样. 若我们的确希望这么写,那按照提示修改即可.但是有时候我们想要显示的就是”$key”,那应该怎么办呢?
个人的话,会这样写:
scala> val key="value"; println(s"$$key") $key key: String = value
scala有人开了个类似的issue,不过没人回答.不知道是否会有别人有更好的solution,可以点击这个地址围观.
上面这个在 IntelliJ IDEA 下会被坑,后来我老老实实在 scalacOptions 里面加上一个开关关掉了这个(当然也会关掉一些关于这个的有用的提示…)
object CompileSettings extends AutoPlugin {
override def trigger = allRequirements
override def projectSettings = Seq(
scalacOptions ++= Seq(
"-optimise",
"-deprecation", // Emit warning and location for usages of deprecated APIs.
"-feature", // Emit warning and location for usages of features that should be imported explicitly.
"-unchecked", // Enable additional warnings where generated code depends on assumptions.
"-Xfatal-warnings", // Fail the compilation if there are any warnings.
"-Xlint", // Enable recommended additional warnings.
"-Ywarn-adapted-args", // Warn if an argument list is modified to match the receiver.
"-Ywarn-dead-code", // Warn when dead code is identified.
"-Ywarn-inaccessible", // Warn about inaccessible types in method signatures.
"-Ywarn-nullary-override", // Warn when non-nullary overrides nullary, e.g. def foo() over def foo.
"-Ywarn-numeric-widen", // Warn when numerics are widened.
"-Yinline-warnings", //
"-language:postfixOps", // See the Scala docs for value scala.language.postfixOps for a discussion
"-Xlint:-warn-missing-interpolator" // ignore error(warning): possible missing interpolator: detected interpolated identifier
//,"-target:jvm-1.8" // force use jvm 1.8
),
//javacOptions in compile ++= Seq("-target", "1.8", "-source", "1.8"), // force use jvm 1.8
compileOrder in Compile := CompileOrder.Mixed,
compileOrder in Test := CompileOrder.Mixed,
testOptions in Test += Tests.Argument(TestFrameworks.Specs2, "junitxml", "console"),
scalacOptions in Test ~= { (options: Seq[String]) =>
options filterNot (_ == "-Ywarn-dead-code") // Allow dead code in tests (to support using mockito).
},
parallelExecution in Test := false,
unmanagedBase := baseDirectory.value / "project/lib")
}
补充: 前面 “-Xlint:-warn-missing-interpolator” 这个好像版本相关?