Scala | Type Inference
Last Updated :
04 Aug, 2022
Scala Type Inference makes it optional to specify the type of variable provided that type mismatch is handled. With type inference capabilities, we can spend less time having to write out things compiler already knows. The Scala compiler can often infer the type of an expression so we don’t have to declare it explicitly. Let us first have a look at the syntax of how immutable variables are declared in scala.
val variable_name : Scala_data_type = value
Example:
Scala
object Geeks
{
def main(args : Array[String])
{
val a : Double = 7.893
println(a)
println(a.getClass)
}
}
|
Output :
7.893
double
In above example the getClass method is used to print the type of the variable to the console. For the above example the type of variable ‘a’ is double. However, through type inference, Scala automatically detects the type of the variable without explicitly specified by the user. Example :
Scala
object Geeks {
def main(args : Array[String])
{
println("Scala Data Types")
val number = 5
val big _ number = 100000000 L
val small _ number = 1
val double _ number = 2.50
val float _ number = 2.50 f
val string _ of _ characters = "This is a string of characters"
val byte = 0xc
val character = 'D'
val empty = ()
println(number)
println(big _ number)
println(small _ number)
println(double _ number)
println(float _ number)
println(string _ of _ characters)
println(byte)
println(character)
println(empty)
}
}
|
Output :
Scala Data Types
5
100000000
1
2.5
2.5
This is a string of characters
12
D
()
Note that no data type is specified explicitly for the above variables. Scala Type Inference For Functions Now we will have a look at how type inference works for functions in Scala. Let’s first have a look at how functions are declared in Scala. Syntax:
def function_name ([parameter_list]) : [return_type] = {
// function body
}
Example :
Scala
object Geeks
{
def main(args : Array[String])
{
println("Product of the two numbers is : " + Prod( 5 , 3 ));
}
def Prod(x : Int, y : Int) : Int =
{
return x*y
}
}
|
Output :
Product of the two numbers is: 15
In above example as we can from the declaration that the specified return type is Int. With Scala type inference, Scala automatically detects the type of the function without explicitly specified by the user. Example :
Scala
object Geeks
{
def factorial(n : Int) = {
var f = 1
for (i <- 1 to n)
{
f = f * i;
}
f
}
def main(args : Array[String])
{
println(factorial( 5 ))
}
}
|
Output :
120
def factorial(n: Int)=
In above example colon and the return type are omitted. Also, In above example, we have omitted the statement “return f” to “f” as we have not specified the return type. If instead of “f”, “return f” was placed then the following error is shown by the compiler.
prog.scala:11: error: method factorial has return statement; needs result type
return f
^
This shows the power of the Scala type inference, but for recursive methods, the compiler is not able to infer a result type. The above factorial function can also be implemented recursively. The following is a recursive definition of the factorial function with no type inference. Example :
Scala
object Geeks {
def factorial(n : Int) : Int =
{
if (n == 0 )
return 1
else
return n * factorial(n- 1 )
}
def main(args : Array[String])
{
println(factorial( 5 ))
}
}
|
Output :
120
Example : With Scala type inference
Scala
object Geeks
{
def factorial(n : Int) =
{
if (n == 0 )
1
else
n * factorial(n- 1 )
}
def main(args : Array[String])
{
println(factorial( 5 ))
}
}
|
Output:
Compile Errors :
prog.scala:8: error: recursive method factorial needs result type
n * factorial(n-1)
^
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...