An Overview Of Scala Type Inference
1. Scala type inference
Let's review the syntax for declaring immutable variables in Scala.
val <Name of our variable> : <Scala type> = <Some literal>
As an example, you can define an immutable variable named donutsToBuy of type Int and assign its value to 5.
println("Step 1: Immutable variable") val donutsToBuy: Int = 5
However, through type inference, Scala complier is smart enough to figure out that the literal 5 is actually an Integer.
As a result, you can simplify the declaration of some immutable variable of type Int as follows:
val donutsBought = 5
2. Declaring Scala Supported Types Using Inference
With type inference in mind, let's redo the data types examples from the tutorial How To Declare Variables And Types.
println("\nStep 2: Scala Types") val donutsBoughtToday = 5 val bigNumberOfDonuts = 100000000L val smallNumberOfDonuts = 1 val priceOfDonut = 2.50 val donutPrice = 2.50f val donutStoreName = "Allaboutscala Donut Store" val donutByte = 0xa val donutFirstLetter = 'D' val nothing = ()
- We did not specify the data types for any of the above variables.
3. Using Scala compiler to convert from one data type to another
The Scala compiler is also smart enough to convert from one data type into another. However, you should bear in mind that this conversion is fine so long as your resulting type is not lossy.
In the example below, say you have numberOfDonuts which is of the type Short. But at some later point in your code, you decide to assign it's value to another immutable variable called minimumDonutsToBuy of type Int.
println("\nStep 3: Using Scala compiler to convert from one data type to another") val numberOfDonuts: Short = 1 val minimumDonutsToBuy: Int = numberOfDonuts
- Converting from a Short to an Int was fine because there was no precision loss, i.e. an Int is larger than a Short.
4. User driven conversion from one data type to another
The example in Step 3 showed how the Scala compiler automatically converted a Short into an Int. But, what would happen if we had a variable minimumDonutsToSell of type String? Let's try it.
println("\nStep 4: User driven conversion from one data type to another ") // NB: You cannot convert from an Int to a String // val minimumDonutsToSell: String = numberOfDonuts
The compiler complains with the following error: type mismatch found Short required String. As we've outlined in the tutorial on the Scala features, Scala's type safety guardrails are here to help us!
So how then would you convert an Int to String? As expected in a fluent functional language, you have access to built in conversion functions. In our example, you can call the toString function on numberOfDonuts:
val minimumDonutsToSell: String = numberOfDonuts.toString()
In this article, we went over the following:
- How Scala is able to infer the type of our variables
- How Scala is able to convert from one data type into another
- How to manually tell Scala to convert one data type into another
- Type inference can be incredibly useful such as to avoid redundant code when initializing collections for some given type.
- But use it wisely. It is a good practice to be clear about the intent of your data type especially when it comes to the return type of a function.
The source code is available on the allaboutscala GitHub repository.
In the next tutorial, I will go over if and else expressions in Scala.
Share this article on