I am definitely more than thrilled with the Scala type inference engine, but in a real world environment:
How much of a performance draw back is it?
When is the type inferred, at compile time or runtime?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With