4 ms·
The idea is that you don't want to call isDefined at all - you'll want to call getOr(T default) or pass it through a chain of methods that are overloaded doFoo(
by mseebach 14y ago
The idea is that you don't want to call isDefined at all - you'll want to call getOr(T default) or pass it through a chain of methods that are overloaded doFoo(Some<T> x) { .. stuff .. return Some<U> }/doFoo(None x) { return None; }
- anonymoushn 14y agoThe 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]