How expensive is type inference?

I am definitely more than thrilled with a Scala type output mechanism, but in a real environment:

  • What part of the performance can be dropped?

  • When is a type called at compile time or runtime?

+7
source share
4 answers

Scala During the compilation process, there are only complex and powerful types (*): they are analyzed from the source (where you give them), displayed, checked, and then finally discarded. The latter may sound pointless, but it is the modus operandi JVM (see type erasure ) and is very useful from the point of view of language designers.

So, in order to answer your question: at runtime, it does not matter whether this type has been explicitly specified or deduced, the difference is how long it takes to compile the program.

(*) In release 2.10, a reflection library will appear that allows the program to access its type of information also at runtime; this gives additional freedom, which when used will, of course, record processor cycles at runtime, but does not change any of the above points.

+18
source

How much of its performance can be discarded?

Runtime performance is generally considered comparable to Java. Compilation time is usually longer compared to Java due to the complexity of the compiler .

When is a type inferred, at compile time or at run time?

At compile time.

In this video, from approximately 8:50 a.m. to 12:50 p.m. Martin touches on real-world use and performance. He notes that many companies use Scala from small startups to large enterprises.

+4
source

This is mentioned in Martin Odersky's answer: Why does IntelliJ IDEA compile Scala so slowly? "The type of output is expensive, in particular if it involves an implicit search."

0
source

The question is that the problem with compilation time in Scala is greater than in Java. It’s hard for me to say that I am not a specialist in compiler compilation. I can say that type inference is obviously not expensive in Kotlin, nor in Groovy2.0 either. What Scala slows down must be something else, such as an implicit search.

-2
source