Type Inference : An helpful feature of Scala

Knoldus Blog Audio
Reading Time: 4 minutes

Scala comes with very interesting features. One such feature is Type Inference. Type Inference is a very powerful feature of Scala as it helps the developer to develop fast and efficiently.

The Scala compiler can often automatically infer the type of an expression (everything in Scala is an expression).

This helps the developer write the code without worrying about mentioning the types which are often known to the compiler.

First of all, let us have a look at how we can declare an immutable variable (i.e. its value cannot be changed after declaring it) in Scala.

val variableName: Scala_type = value

As we see, above is the basic syntax to declare an immutable variable in Scala.

Immutable Variable in Scala

In the above example, we can see as we have declare a variable age with val that means this variable is immutable. The type for this variable is an Int or Integer.

But, with type inference, Scala compiler will automatically infer the type of the variable as Int without explicitly declaring it.

Immutable variable without type

Types are optional in Scala

Scala having such a powerful type inference system makes declaring types in Scala optional, be it for a variable or a function. Also makes your code more readable.

Type inferring for variables

Scala compiler can automatically infer types of each variable declared.

If the value of a variable is declared in double-quotes it will automatically be inferred as String.

type inference of String Variable

Also, the compiler can infer any value in a single quote is inferred as Char.

type inference of Char Variable

The compiler, by default it infer any number without decimal as Int.

type inference of Integer variable

Likewise, the compiler will automatically infer any decimal number as a Double.

type inference of Double Variable

And, as for true and false values the compiler inferred it as Boolean.

type inference of Boolean Variable

Type inferring for Functions

As we have seen in the above examples, the type inference system automatically infers types of variables.

Likewise, it can instinctively infer the result type of a function.

Example:

def sum(numberOne: Int, numberTwo: Int) = numberOne + numberTwo
val result = sum(5, 5)

Output:

val result: Int = 10

In the above example, as we can see for the function sum compiler is inferring the result type as Int as the last expression will give a value of Int type. So in this case we explicitly declaring the result type is not needed.

But, for the recursive method, the compiler is not able to infer the result type.

Example:

def factorial(number: Int) = if (number == 0) 1 else number * factorial(number - 1)

Output:

recursive method factorial needs result type

In the above case, the compiler will fail to infer the return type of factorial function.

It is also not necessary to explicitly give the type parameters when polymorphic methods are called or generic classes are instantiated. The Scala compiler will infer such missing type parameters from the context and from the types of the actual method/constructor parameters.

Type inferring for Parameters

Scala compiler never infers the types for method parameters. But, in some cases, it can infer types of anonymous function parameters when it is passed as arguments.

When to use explicit type declaration?

As we have seen, Type inference may help in saving time and unnecessary efforts for declaring type which may be already known to the compiler.

It may make the code more readable but sometimes there are cases when the explicit declaration is preferred.

As we have seen above there are some cases when the compiler fails to infer the type (recursive functions) or there may be cases when we need a value to have a specific type, in that case, it is preferred to explicitly declare the type in Scala.

References: