Reader small image

You're reading from  Learning Scala Programming

Product typeBook
Published inJan 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781788392822
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Vikash Sharma
Vikash Sharma
author image
Vikash Sharma

Vikash Sharma is a software developer and open source technology evangelist. He tries to keep things simple, which helps him write clean and manageable code. He has invested a large amount of time learning and implementing Scala code, and he has authored video courses for Scala. He works as a developer at SAP Labs.
Read more about Vikash Sharma

Right arrow

Option type


Option is one of the type constructors that Scala provides. The question arises, what's a type constructor? The answer is simple; it lets you construct a type. We'll take two statements:

  1. Option is a type constructor
  2. Option[Int] is a type

Let's discuss these in detail. When I say Foo is a type constructor, it means that Foo expects you to provide a particular type in the form of a parameter. It looks like Foo[T], where T is an actual type. We call them type parameters and we'll talk about them in the following few sections.

In the second statement, we saw that we gave an Int type to our Option type constructor in brackets and it formed a type. If you try this in the Scala REPL, it'll tell you exactly the same thing we discussed:

scala> val a: Option = Some(1) 
<console>:11: error: class Option takes type parameters 
       val a: Option = Some(1) 
 
scala> val a: Option[Int] = Some(1) 
a: Option[Int] = Some(1) 

In simple words, the Option[T] type represents an optional value...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Learning Scala Programming
Published in: Jan 2018Publisher: PacktISBN-13: 9781788392822

Author (1)

author image
Vikash Sharma

Vikash Sharma is a software developer and open source technology evangelist. He tries to keep things simple, which helps him write clean and manageable code. He has invested a large amount of time learning and implementing Scala code, and he has authored video courses for Scala. He works as a developer at SAP Labs.
Read more about Vikash Sharma