Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing inferred types of Scala expressions

Tags:

eclipse

scala

sbt

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.

like image 245
Robin Green Avatar asked Feb 10 '12 15:02

Robin Green


People also ask

What is type inference in Scala?

The Scala compiler can infer the types of expressions automatically from contextual information. Therefore, we need not declare the types explicitly. This feature is commonly referred to as type inference. It helps reduce the verbosity of our code, making it more concise and readable.

How does Scala determine types when they are not specified?

Non-value types capture properties of identifiers that are not values. For example, a type constructor does not directly specify a type of values. However, when a type constructor is applied to the correct type arguments, it yields a first-order type, which may be a value type.

What is .type in Scala?

Scala is a statically typed programming language. This means the compiler determines the type of a variable at compile time. Type declaration is a Scala feature that enables us to declare our own types.


1 Answers

It needs to be

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

instead.

Unfortunately the output isn't very readable. :(

like image 148
Robin Green Avatar answered Oct 15 '22 18:10

Robin Green