Showing inferred types of Scala expressions

自闭症网瘾萝莉.ら 提交于 2020-01-01 05:01:05

问题


How can I see the types inferred by the Scala compiler for expressions etc.? I have some code with complicated type inference and implicit conversions, and it's hard to see what's going on just by reading the code.

I've tried adding

scalacOptions in Compile += "-Xprint-types"

in build.sbt, but this has no effect.

Using scalac directly isn't very appealing because I have lots of dependencies.

I use the Eclipse Scala plugin and ENSIME to write code, and SBT to build.


回答1:


It needs to be

scalacOptions in Compile ++= Seq("-Xprint-types", "-Xprint:typer")

instead.

Unfortunately the output isn't very readable. :(




回答2:


This exact feature has been added in Eclipse Scala IDE 3.0!

Select any portion of code and press Ctrl-Shift-W T (replacing Ctrl by Cmd on Mac) to see the inferred type.




回答3:


Hoist the expression to a non-local def or val, without an explicit type - then it will appear in the Outline view in Eclipse, with an inferred type assigned.

However, this isn't an ideal solution because it requires some work, and it can't be used when recursion is involved.



来源:https://stackoverflow.com/questions/9230185/showing-inferred-types-of-scala-expressions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!