是否有一种系统的方法来发现哪些隐式定义在范围内,以及哪些隐式定义在特定点绑定?

发布于 2024-09-08 02:22:05 字数 487 浏览 1 评论 0原文

通常不需要关注 Scala 中的隐式参数,但有时了解编译器如何自动提供它们非常有帮助。不幸的是,这种理解似乎很难获得!

是否有一种通用方法可以发现在给定的代码段中如何提供隐式参数?

理想情况下,有一天 IDE 集成会以某种方式提供此信息,但我希望现在我'必须更深入地挖掘。是否有某种方法可以要求编译器准确解释它在任何给定点选择的隐式定义?这可以从其他编译器输出间接破译吗?

举个例子,我想知道如何自己解决 TraversableLike.mapimplicit bf: CanBuildFrom[Repr, B, That] 参数出现的情况没有阅读 Stack Overflow 上的这个之类的问题!

Often there's no need to pay any attention to implicit arguments in Scala, but sometimes it's very helpful to understand how the compiler is automatically providing them. Unfortunately, this understanding seems to be hard to obtain!

Is there a general method to discover how an implicit parameter has been provided, in a given piece of code?

Ideally, one day IDE integration would provide this information in some way, but I expect for now I'll have to dig deeper. Is there some way to ask the compiler to explain exactly which implicit definition it chooses at any given point? Can this be deciphered indirectly from other compiler output?

As an example, I'd like to know how to work out on my own where the implicit bf: CanBuildFrom[Repr, B, That] argument to TraversableLike.map comes from, without reading questions like this one on Stack Overflow!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

暗恋未遂 2024-09-15 02:22:05
  1. 将选项 -Xprint:typer 添加到 scalac 命令行。这会在打字机编译阶段之后打印程序树。这最适合一个简短的、独立的示例。您也可以将其传递给 scalac。这确实是 Scala 迈向自力更生的一大步!
  2. 正如 Randall 提到的,IntelliJ 使用 CTRL-ALT-SHIFT-I 显示范围内和选定的隐式视图。等一两个月,隐含的论点可能会得到类似的支持。
  1. Add the option -Xprint:typer to the scalac command line. This prints the program tree just after the typer compiler phase. This works best with a short, self contained example. You can also pass this to scalac. This is a really huge step towards self-reliance in Scala!
  2. As mentioned by Randall, IntelliJ shows in-scope and the selected Implicit View with CTRL-ALT-SHIFT-I. Wait a month or two and implicit arguments are likely to have similar support.
爺獨霸怡葒院 2024-09-15 02:22:05

理想情况下,有一天 IDE 集成会以某种方式提供此信息,...

这一天就是今天的 JetBrains IDEA。如果您使用最新的每晚发布的 Scala 插件运行 IDEA 版本 9 (9.0.3 EA #95.289) 的最新 EAP,则存在此功能。可以选择每个值表达式并发出一条命令,该命令显示一个弹出窗口,其中显示所有适用的隐式转换,并突出显示编译器将选择的隐式转换。

而且显然还有一些人还不知道 IDEA 有一个免费的开源社区版,并且它确实支持 Scala 插件。

Ideally, one day IDE integration would provide this information in some way, ...

That day is today in with JetBrains' IDEA. If you run the latest EAP of IDEA version 9 (9.0.3 EA #95.289) with a recent nightly release of the Scala plug-in, this capability is present. Every value expression may be selected and a command issued that displays a pop-up showing all applicable implicit conversions with the one the compiler will select highlighted.

And since there are apparently a few out there who don't yet know it, there is a free and open-source Community Edition of IDEA and it does support the Scala plug-in.

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