summaryrefslogtreecommitdiff
path: root/src/library/scala/Option.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-11-01 17:29:19 +0000
committerPaul Phillips <paulp@improving.org>2009-11-01 17:29:19 +0000
commitb42e1f1902c47f4b5fef0be17f5a5c606ac6a777 (patch)
tree21ead34d9a4c8c076316cef18fc97289f8b6d8a3 /src/library/scala/Option.scala
parent52f14327c23d246e041c64f8fc4e63894101ba73 (diff)
downloadscala-b42e1f1902c47f4b5fef0be17f5a5c606ac6a777.tar.gz
scala-b42e1f1902c47f4b5fef0be17f5a5c606ac6a777.tar.bz2
scala-b42e1f1902c47f4b5fef0be17f5a5c606ac6a777.zip
Some structural improvements to Either and Opti...
Some structural improvements to Either and Option which leverage recent awesomeness in constraining type parameters. In Either I was able to define joinLeft and joinRight on the instance rather than on the object, and while I didn't manage that directly with merge, it can at least be accomplished via implicit as endorsed by martin 25/Jun/09 on scala-internals.
Diffstat (limited to 'src/library/scala/Option.scala')
-rw-r--r--src/library/scala/Option.scala20
1 files changed, 7 insertions, 13 deletions
diff --git a/src/library/scala/Option.scala b/src/library/scala/Option.scala
index a834623a37..510fb6c05b 100644
--- a/src/library/scala/Option.scala
+++ b/src/library/scala/Option.scala
@@ -30,19 +30,6 @@ object Option
*/
@experimental
def apply[A](x: A): Option[A] = if (x == null) None else Some(x)
-
- // For methods which return -1 on failure
- // def fromReturnValue(value: Int): Option[Int] = if (value < 0) None else Some(value)
-
- class NullableOption[A >: Null <: AnyRef](x: Option[A]) {
- /** The option's value if it is nonempty, or <code>null</code> if it is empty.
- * The use of null of course is discouraged, but code written to use Options
- * often must interface with code which expects and returns nulls.
- */
- @experimental
- def orNull: A = if (x.isEmpty) null else x.get
- }
- implicit def option2NullableOption[A >: Null <: AnyRef](xo: Option[A]): NullableOption[A] = new NullableOption(xo)
}
/** This class represents optional values. Instances of <code>Option</code>
@@ -89,6 +76,13 @@ sealed abstract class Option[+A] extends Product {
def orZero[B >: A](implicit z: Zero[B]): B =
this getOrElse z.zero
+ /** The option's value if it is nonempty, or <code>null</code> if it is empty.
+ * The use of null of course is discouraged, but code written to use Options
+ * often must interface with code which expects and returns nulls.
+ */
+ @experimental
+ def orNull[A1 >: A](implicit ev: Null <:< A1): A1 = this getOrElse null
+
/** If the option is nonempty, return a function applied to its value,
* wrapped in a Some i.e. <code>Some(f(this.get))</code>.
* Otherwise return <code>None</code>.