Type Inference
In the following lesson, you will be introduced to type inference and learn about Scala's ability to infer data types.
Type inference is Scala’s ability to infer types when not specified by the user. To better understand how this works, let’s look at the example we used in the first lesson of this chapter where we had to declare variables for the author, title, and number of pages in a book.
Scala
val bookTitle: String = "Lord of the Rings: The Fellowship of the Ring"val bookAuthor: String = "J. R. R. Tolkien"val bookNoOfPages: Int = 423// Driving Codeprintln(bookTitle)println(bookAuthor)println(bookNoOfPages)
In the code above, we are using the following syntax:
However, Scala’s type inference feature allows us to declare a variable without explicitly mentioning the data ...