Re:Why we've banned null in our codebase
by mseebach on Friday January 04, 2013 @12:15PM
(#5008566)
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; }
[ Parent
| on HN ]
Re:Why we've banned null in our codebase
by anonymoushn on Friday January 04, 2013 @12:25PM
(#5008623)
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]