Display Scala Expression Type Outputs

How can I see the types output by the Scala compiler for expressions, etc.? I have code with a complex type of output and an implicit conversion, and it's hard to understand what is happening just by reading the code.

I tried adding

scalacOptions in Compile += "-Xprint-types" 

in build.sbt, but this has no effect.

Using scalac directly is not very attractive because I have a lot of dependencies.

I use the Eclipse Scala plugin and ENSIME for writing code and SBT for building.

+8
eclipse scala sbt
source share
4 answers

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

Select any part of the code and press Ctrl-Shift-W T (replacing Ctrl with Cmd on Mac) to see the intended type.

+2
source share

It should be

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

instead.

Unfortunately, the output is not very readable. :(

+4
source share

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

However, this is not an ideal solution because it requires some work and cannot be used in recursion.

0
source share

I prepared a question on this issue. Probably the best I put it in here (please don't cry because that is not the answer).

I tried Robin Green's solution, but (as you know) it gives more output than required.

Here:

Being new to Scala and a mechanism of the supposed type, I think it would be useful to do something like this:

 @spitType s= something ... 

The imaginary spitType will look like a compile-time macro (similar to #pragma warning in C ++), which would give information about the type of expression at compile time.

I know that I can get this information at runtime, but in cases where the rest of the code is not compiled yet, a compilation message will be the only useful thing.

Could you find this opportunity useful or simply inappropriate?

0
source share

All Articles