Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How expensive is type inference?

Tags:

scala

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?

like image 488
flavian Avatar asked Dec 09 '12 05:12

flavian


People also ask

Does Swift have type inference?

If you don't specify the type of value you need, Swift uses type inference to work out the appropriate type. Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide.

What is type inferred language?

Type inference is the ability to automatically deduce, either partially or fully, the type of an expression at compile time. The compiler is often able to infer the type of a variable or the type signature of a function, without explicit type annotations having been given.

What is type inference Kotlin?

Local type inference in Kotlin is the process of deducing the compile-time types of expressions, lambda expression parameters and properties. As previously mentioned, type inference is a type constraint problem, and is usually solved by a type constraint solver.

What does type inference mean 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.


2 Answers

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.

like image 82
Roland Kuhn Avatar answered Nov 18 '22 10:11

Roland Kuhn


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.

like image 28
Brian Avatar answered Nov 18 '22 08:11

Brian