3 ms·
The latter won't work for you as-is: scala> def doFoo(x: Some[Int]) = x.get*2 doFoo: (x: Some[Int])Int scala> def doFoo(x: None.type) = None doFoo: (x
by anonymoushn 14y ago
The latter won't work for you as-is:
scala> def doFoo(x: Some[Int]) = x.get*2
doFoo: (x: Some[Int])Int
scala> def doFoo(x: None.type) = None
doFoo: (x: None.type)None.type
scala> doFoo(Option(3))
<console>:9: error: type mismatch;
found : Option[Int]
required: None.type
doFoo(Option(3))
^
But it will work as either of these:
scala> def doFoo(x: Option[Int]): Option[Int] = x match {
| case Some(n) => Some(n*2)
| case _ => None
| }
scala> def doFoo(x: Option[Int]) = x.map(_*2)
doFoo: (x: Option[Int])Option[Int]