哪些 Scala 注释会修改编译器的消息?

发布于 2024-10-07 04:42:58 字数 487 浏览 1 评论 0原文

我知道两个:

  • @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 at CanBuildFrom, msg can contain placeholders of the type ${A} if A 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小霸王臭丫头 2024-10-14 04:42:58

@migration-Xmigration 一起使用来指示方法从一个版本到另一个版本的语义更改,以帮助在版本之间移植代码。

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

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.

@migration(2, 8, "As of 2.8, keys returns Iterable[A] rather than Iterator[A].")
忆离笙 2024-10-14 04:42:58

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

There is @tailrec, which makes the compiler output an error if tail call optimization cannot be applied to the annotated method.

情话已封尘 2024-10-14 04:42:58

从 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.”

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文