哪些 Scala 注释会修改编译器的消息?
我知道两个:
@deprecated("use blabla 相反")
用于在客户端代码中使用带注释的定义时向编译器的警告输出添加解释。@implicitNotFound(msg = "more有意义的解释")
用于在找不到带注释的定义类型的隐含类型时输出附加错误消息。查看CanBuildFrom
,如果A
是带注释类型的类型参数的名称,则 msg 可以包含${A}
类型的占位符,由编译器用实际期望的类型填充,例如:@implicitNotFound(msg = "无法基于 ${To} 类型的集合构造具有 ${Elem} 类型元素的 ${To} 类型集合。") 特征 CanBuildFrom[-From, -Elem, +To] { ... }
还有其他这样的注释吗?
I know about two:
@deprecated("use blabla instead")
is used to add an explanation to the warning output by the compiler when the annotated definition is used in client code.@implicitNotFound(msg = "more meaningful explanation")
is used to output an additional error message whenever an implicit of the type of the annotated definition cannot be found. Looking atCanBuildFrom
, msg can contain placeholders of the type${A}
ifA
is the name of a type parameter of the annotated type, which is filled in by the compiler with the actual expected type, e.g.:@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] { ... }
Are there any other such annotations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@migration
与-Xmigration
一起使用来指示方法从一个版本到另一个版本的语义更改,以帮助在版本之间移植代码。There is
@migration
, which is used with-Xmigration
to indicate semantic changes in methods from one version to another, in helping port code between versions.有@tailrec,如果尾调用优化不能应用于带注释的方法,它会使编译器输出错误。
There is @tailrec, which makes the compiler output an error if tail call optimization cannot be applied to the annotated method.
从 Scala 2.9 开始,还有
@deprecatedName
:“一个注释,指定其所应用的参数的名称已被弃用。在命名参数中使用该名称会生成弃用警告。”As of Scala 2.9, there's also
@deprecatedName
: “An annotation that designates the name of the parameter to which it is applied as deprecated. Using that name in a named argument generates a deprecation warning.”