diff options
author | Matthias Zenger <mzenger@gmail.com> | 2003-10-03 13:52:47 +0000 |
---|---|---|
committer | Matthias Zenger <mzenger@gmail.com> | 2003-10-03 13:52:47 +0000 |
commit | 4f7353b4470a974e7c96df3be869839a19102ba9 (patch) | |
tree | 27ad56a43c3e4173d9ed1128da73b87279bb315f /sources/scala/Option.scala | |
parent | 82f735e5d5e9275a183d085c6b9d2ee01e701551 (diff) | |
download | scala-4f7353b4470a974e7c96df3be869839a19102ba9.tar.gz scala-4f7353b4470a974e7c96df3be869839a19102ba9.tar.bz2 scala-4f7353b4470a974e7c96df3be869839a19102ba9.zip |
*** empty log message ***
Diffstat (limited to 'sources/scala/Option.scala')
-rw-r--r-- | sources/scala/Option.scala | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/sources/scala/Option.scala b/sources/scala/Option.scala index b037a3ef04..25030ab578 100644 --- a/sources/scala/Option.scala +++ b/sources/scala/Option.scala @@ -25,37 +25,42 @@ trait Option[+A] extends Iterable[A] { case _ => false } - def get: A = this match { + def get: A = match { case None => error("None.get") case Some(x) => x } - def map[B](f: A => B): Option[B] = this match { + def get[B >: A](default: B): B = match { + case None => default + case Some(x) => x + } + + def map[B](f: A => B): Option[B] = match { case None => None case Some(x) => Some(f(x)) } - def flatMap[B](f: A => Option[B]): Option[B] = this match { + def flatMap[B](f: A => Option[B]): Option[B] = match { case None => None case Some(x) => f(x) } - def filter(p: A => Boolean): Option[A] = this match { + def filter(p: A => Boolean): Option[A] = match { case None => None case Some(x) => if (p(x)) Some(x) else None } - def foreach(f: A => Unit): Unit = this match { + def foreach(f: A => Unit): Unit = match { case None => () case Some(x) => f(x) } - def elements: Iterator[A] = this match { + def elements: Iterator[A] = match { case None => Iterator.empty case Some(x) => Iterator.fromSeq(x) } - def toList: List[A] = this match { + def toList: List[A] = match { case None => Predef.List() case Some(x) => Predef.List(x) } |