是否有一种系统的方法来发现哪些隐式定义在范围内,以及哪些隐式定义在特定点绑定?
通常不需要关注 Scala 中的隐式参数,但有时了解编译器如何自动提供它们非常有帮助。不幸的是,这种理解似乎很难获得!
是否有一种通用方法可以发现在给定的代码段中如何提供隐式参数?
理想情况下,有一天 IDE 集成会以某种方式提供此信息,但我希望现在我'必须更深入地挖掘。是否有某种方法可以要求编译器准确解释它在任何给定点选择的隐式定义?这可以从其他编译器输出间接破译吗?
举个例子,我想知道如何自己解决 TraversableLike.map
的 implicit 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
-Xprint:typer
添加到 scalac 命令行。这会在打字机编译阶段之后打印程序树。这最适合一个简短的、独立的示例。您也可以将其传递给 scalac。这确实是 Scala 迈向自力更生的一大步!CTRL-ALT-SHIFT-I
显示范围内和选定的隐式视图。等一两个月,隐含的论点可能会得到类似的支持。-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!CTRL-ALT-SHIFT-I
. Wait a month or two and implicit arguments are likely to have similar support.这一天就是今天的 JetBrains IDEA。如果您使用最新的每晚发布的 Scala 插件运行 IDEA 版本 9 (9.0.3 EA #95.289) 的最新 EAP,则存在此功能。可以选择每个值表达式并发出一条命令,该命令显示一个弹出窗口,其中显示所有适用的隐式转换,并突出显示编译器将选择的隐式转换。
而且显然还有一些人还不知道 IDEA 有一个免费的开源社区版,并且它确实支持 Scala 插件。
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.