7

I am definitely more than thrilled with the Scala type inference engine, but in a real world environment:

  1. How much of a performance draw back is it?

  2. When is the type inferred, at compile time or runtime?

flavian
  • 28,161
  • 11
  • 65
  • 105

4 Answers4

18

Scala’s elaborate and powerful types exist only(*) during compilation time: they are parsed from source (where you give them), inferred, checked and then, finally, discarded. The last may sound nonsensical, but it is the modus operandi of the JVM (see type erasure) and quite useful from a language designer’s viewpoint.

So, to answer your question: at runtime it makes no difference whether the type was explicitly given or inferred, the only difference is in how long it takes to compile the program.

(*) The 2.10 release will come with a reflection library which allows the program to access its type information also at runtime; this gives added freedom—which if used will of course burn CPU cycles at runtime—but does not change any of the aforementioned points.

Roland Kuhn
  • 15,412
  • 2
  • 36
  • 45
4

How much of a performance draw back is it?

The run time performance is generally said to be comparable with Java. Compilation times are typically longer compared to Java due to the complexity of the compiler.

When is the type inferred, at compile time or runtime?

At compile time.

In this video from about 8:50 to 12:50 Martin touches on usage in "real world" environments and performance. He notes there are many companies using Scala from small startups to large enterprises.

Community
  • 1
  • 1
Brian
  • 20,195
  • 6
  • 34
  • 55
  • 5
    You should make it clearer that type inference does not have a negative effect on performance at all. – Kim Stebel Dec 09 '12 at 07:28
  • @KimStebel good point, in fact, the only tangible performance negative occurs at compile time; the compiler itself is of course totally unaffected, it could care less about you sitting there waiting. – virtualeyes Dec 09 '12 at 17:08
0

This is mentioned in this answer by Martin Odersky : Why does IntelliJ IDEA compile Scala so slowly? "Type inference is costly, in particular if it involves implicit search."

Community
  • 1
  • 1
blue-sky
  • 51,962
  • 152
  • 427
  • 752
-2

The question is where the problem with compile times in Scala being longer than in Java come from. Hard for me to say as I'm no compiler construction expert. What I can say is that type inference is obviously not costly in Kotlin and also not in Groovy2.0. What slows down Scala must be something else such as search for implicits.

OlliP
  • 1,545
  • 11
  • 22
  • 1
    Groovy is dynamically typed, and does not use type inference. All types are resolved at runtime. Scala resolves all types at compile time. – Yann Ramin Dec 09 '12 at 20:10
  • No, not since Groovy2.0 where you can have a method or a class >statically< typed if you annotate it with @CompileStatic. When you do so, you don't have to declare the type. Groovy can infer it in most cases. See [link] (http://www.infoq.com/articles/new-groovy-20) – OlliP Dec 11 '12 at 10:20
  • What makes Scala take longer to compile than other languages is the search for implicits and the very strict type system where a lot more of matching work has to be done than in languages with a weaker type system. – OlliP Dec 11 '12 at 10:21
  • Cumbersome you get downrated that heavily only because someone doesn't know that Groovy meanwhile has type inference and optional static typing ... – OlliP Dec 13 '12 at 10:32
  • By the way, I realized some days ago that even Go has some degree of type inference. And no.#1 priority in Go is short build times! At runtime type inference doesn't matter as the compiler has infered them at compile time and thus have been deduced once and forever for the current build. – OlliP Feb 11 '13 at 08:58