有没有系统的方法来发现哪些隐含的def在范围内,哪一个在一个特定的点? [英] Is there a systematic way to discover which implicit defs are in scope, and which one is bound at a particular point?

查看:172
本文介绍了有没有系统的方法来发现哪些隐含的def在范围内,哪一个在一个特定的点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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!


有一个通用的方法来发现如何提供一个隐式参数,在给定的代码中?

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

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?

例如,我想知道如何在自己的工作,其中隐藏的bf:CanBuildFrom [Repr,B,That] 来自 TraversableLike.map 的参数没有阅读这一个在Stack Overflow上!

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!

推荐答案


  1. 将选项 -Xprint:typer 添加到scalac命令行。这将在typer编译器阶段之后打印程序树。这最好用一个简短的,自包含的例子。你也可以把它传递给scalac。

  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.

这篇关于有没有系统的方法来发现哪些隐含的def在范围内,哪一个在一个特定的点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆