在metrics-scala库中,我们有以下方法:
def timer(name: String, scope: String = null): Timer
Run Code Online (Sandbox Code Playgroud)
我想弃用scope参数并将其从下一个主要版本中删除.
我试过这个:
def timer(name: String): Timer
@deprecated(...)
def timer(name: String, scope: String): Timer
Run Code Online (Sandbox Code Playgroud)
但这导致当前主要版本中已存在二进制向后兼容性问题(见下文*).
我也试过这个:
def timer(name: String, @deprecated(...) scope: String = null): Timer
Run Code Online (Sandbox Code Playgroud)
但是这会在内部发出警告timer,而不是对呼叫者发出警告timer.
我是否遗漏了某些内容,或者是否真的无法使用默认值弃用参数?
(*)选项1的Mima报告:
sbt:metrics4-scala-root> mimaReportBinaryIssues
[error] * synthetic method timer$default$2()java.lang.String in class nl.grons.metrics4.scala.MetricBuilder does not have a correspondent in current version
[error] filter with: ProblemFilters.exclude[DirectMissingMethodProblem]("nl.grons.metrics4.scala.MetricBuilder.timer$default$2")
Run Code Online (Sandbox Code Playgroud)
我相信(但我现在没有 MiMa 设置来检查)您可以使用特征:
object Foo extends DeprecatedFoo {
def timer(name: String): Unit = { println("called new shiny version") }
}
trait DeprecatedFoo {
@deprecated("", "")
def timer(name: String, scope: String = null) = { println("called bad old version")}
}
Foo.timer("xx") // calls new version
Foo.timer("xx", null) // calls old version and issues a warning:
Run Code Online (Sandbox Code Playgroud)
为旧版本编译的代码将会执行invokevirtual Foo/timer(Ljava/lang/String;Ljava/lang/String;)Z,这也会解析为旧版本。