什么Scala注释修改编译器的消息?

时间:2010-12-10 15:35:27

标签: scala annotations compiler-errors compiler-warnings

我知道两个:

  • @deprecated("use blabla instead")用于在客户端代码中使用带注释的定义时向编译器添加对警告输出的解释。
  • @implicitNotFound(msg = "more meaningful explanation")用于在无法找到带注释的定义类型的隐式时输出其他错误消息。查看CanBuildFrom,如果${A}是带注释类型的类型参数的名称,则msg可以包含类型为A的占位符,编译器将使用实际预期类型填充该类型参数,例如:

    @implicitNotFound(msg = "Cannot construct a collection of type ${To} with elements of type ${Elem} based on a collection of type ${To}.")
    trait CanBuildFrom[-From, -Elem, +To] { ... }
    

还有其他此类注释吗?

3 个答案:

答案 0 :(得分:6)

@migration-Xmigration一起用于指示方法从一个版本到另一个版本的语义更改,以帮助版本之间的端口代码。

@migration(2, 8, "As of 2.8, keys returns Iterable[A] rather than Iterator[A].")

答案 1 :(得分:6)

有@tailrec,如果尾调用优化不能应用于带注释的方法,则会使编译器输出错误。

答案 2 :(得分:3)

从Scala 2.9开始,还有@deprecatedName:“一个注释,用于指定将其应用为已弃用的参数的名称。在命名参数中使用该名称会生成弃用警告。“