By Alvin Alexander. Last updated: January 14, 2024
As I note in How to use fold on a Scala Option, if you’re using Scala and need to create a None
of a specific data type, either of these two approaches will solve the problem:
val none = Option.empty[Int] // Option[Int]
val none: Option[Int] = None // Option[Int]
Both of those approaches create the None
with the type Option[Int]
, as shown in the Scala REPL:
scala> val none = Option.empty[Int] val none: Option[Int] = None scala> val none: Option[Int] = None val none: Option[Int] = None
I tweeted about this here on Twitter, and also wrote about it here on LinkedIn, where Rob Moore mentioned the second approach.
In summary, if you ever need to create a None
of a specific data type in Scala, I hope this helps.