An Overview Of Scala Type Inference

By Nadim Bahadoor | Last updated: July 25, 2017 at 13:35 pm

Overview

In this tutorial, we will build on what we've previously learned from Tutorial 1 regarding how to declare variables in Scala.

 

More specifically, we will redo the examples from Tutorial 1 and show how Scala is able to infer the data types for our variables.

Step

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 = ()

NOTE:

  • 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

NOTE:

  • 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()

This concludes our tutorial on An Overview Of Scala Type Inference and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

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

Tip

  • 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.

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will go over if and else expressions in Scala. 

 

Stay tuned!

 

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Elsevier
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on