加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在Scala中使用默认值弃用参数?

发布时间:2020-12-16 18:32:09 所属栏目:安全 来源:网络整理
导读:在metrics- scala库中,我们有以下方法: def timer(name: String,scope: String = null): Timer 我想弃用scope参数并将其从下一个主要版本中删除. 我试过这个: def timer(name: String): Timer@deprecated(...)def timer(name: String,scope: String): Time
在metrics- scala库中,我们有以下方法:

def timer(name: String,scope: String = null): Timer

我想弃用scope参数并将其从下一个主要版本中删除.

我试过这个:

def timer(name: String): Timer
@deprecated(...)
def timer(name: String,scope: String): Timer

但这导致当前主要版本中已存在二进制向后兼容性问题(见下文*).

我也试过这个:

def timer(name: String,@deprecated(...) scope: String = null): 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")

解决方法

我相信(但我现在没有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:

为旧版本编译的代码将执行invokevirtual Foo / timer(Ljava / lang / String; Ljava / lang / String;)Z,它也将解析为旧版本.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读