是否可以在scala中注释lambda?

时间:2016-02-12 02:11:24

标签: scala annotations dsl

对于我的dsl,我需要一些精神:

@deprecated def foo(x: Int) = x

...仅适用于lambdas \ anonymous functions。

这样的事情可能吗?

1 个答案:

答案 0 :(得分:1)

显然,这种语言存在于lang spec

的语言中
  

表达式e的注释出现在表达式e之后,   用冒号隔开。

所以这应该有效:

object TestAnnotation {
  val o = Some(1)

  def f = o.map(_ + 1 : @deprecated("gone", "forever"))

  val e = { 1 + 2 } : @deprecated("hmm", "y")

  println(f)
  println(e)
}

然而,当我用scalac -deprecation编译它时,我得不到任何警告。我打开了一个问题here并得到了一个不受支持的回复。

您可以使用的一种解决方法是单独声明lambda:

object TestAnnotation {
  val o = Some(1)

  @deprecated("this", "works") val deprecatedLambda: Int => Int = _ + 1

  o.map(deprecatedLambda)
}

scalac然后给出:

Annotation.scala:6: warning: value deprecatedLambda in object TestAnnotation is deprecated: this
  o.map(deprecatedLambda)
        ^
one warning found
相关问题