diff options
author | Paul Phillips <paulp@improving.org> | 2011-06-06 23:53:24 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-06-06 23:53:24 +0000 |
commit | 29cdb5837ca05a883093bb0e7ddfac264c84afea (patch) | |
tree | 01a126027568115f8017b024f113aafafd1ea8e7 /src | |
parent | b9731954fbdb7727b59d47095191638c53e3a2e7 (diff) | |
download | scala-29cdb5837ca05a883093bb0e7ddfac264c84afea.tar.gz scala-29cdb5837ca05a883093bb0e7ddfac264c84afea.tar.bz2 scala-29cdb5837ca05a883093bb0e7ddfac264c84afea.zip |
Realized most of the remaining methods in Optio...
Realized most of the remaining methods in Option warrant @inline and
final treatment and modified them accordingly. Review by dragos.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/Option.scala | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/src/library/scala/Option.scala b/src/library/scala/Option.scala index 81a079f723..63e5582a99 100644 --- a/src/library/scala/Option.scala +++ b/src/library/scala/Option.scala @@ -30,10 +30,9 @@ object Option { /** Represents optional values. Instances of `Option` * are either an instance of $some or the object $none. * - * The most idiomatic way to use an $option instance - * is to treat it as a collection or monad and - * use `map`,`flatMap`, `filter`, - * or `foreach`: + * The most idiomatic way to use an $option instance is to treat it + * as a collection or monad and use `map`,`flatMap`, `filter`, or + * `foreach`: * * {{{ * val name:Option[String] = request.getParameter("name") @@ -130,7 +129,7 @@ sealed abstract class Option[+A] extends Product with Serializable { * @see flatMap * @see foreach */ - def map[B](f: A => B): Option[B] = + @inline final def map[B](f: A => B): Option[B] = if (isEmpty) None else Some(f(this.get)) /** Returns the result of applying $f to this $option's value if @@ -143,7 +142,7 @@ sealed abstract class Option[+A] extends Product with Serializable { * @see map * @see foreach */ - def flatMap[B](f: A => Option[B]): Option[B] = + @inline final def flatMap[B](f: A => Option[B]): Option[B] = if (isEmpty) None else f(this.get) def flatten[B](implicit ev: A <:< Option[B]): Option[B] = @@ -154,7 +153,7 @@ sealed abstract class Option[+A] extends Product with Serializable { * * @param p the predicate used for testing. */ - def filter(p: A => Boolean): Option[A] = + @inline final def filter(p: A => Boolean): Option[A] = if (isEmpty || p(this.get)) this else None /** Returns this $option if it is nonempty '''and''' applying the predicate $p to @@ -162,7 +161,7 @@ sealed abstract class Option[+A] extends Product with Serializable { * * @param p the predicate used for testing. */ - def filterNot(p: A => Boolean): Option[A] = + @inline final def filterNot(p: A => Boolean): Option[A] = if (isEmpty || !p(this.get)) this else None /** Necessary to keep $option from being implicitly converted to @@ -187,7 +186,7 @@ sealed abstract class Option[+A] extends Product with Serializable { * * @param p the predicate to test */ - def exists(p: A => Boolean): Boolean = + @inline final def exists(p: A => Boolean): Boolean = !isEmpty && p(this.get) /** Apply the given procedure $f to the option's value, @@ -197,7 +196,7 @@ sealed abstract class Option[+A] extends Product with Serializable { * @see map * @see flatMap */ - def foreach[U](f: A => U) { + @inline final def foreach[U](f: A => U) { if (!isEmpty) f(this.get) } @@ -218,7 +217,7 @@ sealed abstract class Option[+A] extends Product with Serializable { * otherwise return the result of evaluating `alternative`. * @param alternative the alternative expression. */ - def orElse[B >: A](alternative: => Option[B]): Option[B] = + @inline final def orElse[B >: A](alternative: => Option[B]): Option[B] = if (isEmpty) alternative else this /** Returns a singleton iterator returning the $option's value @@ -241,7 +240,7 @@ sealed abstract class Option[+A] extends Product with Serializable { * @param left the expression to evaluate and return if this is empty * @see toLeft */ - def toRight[X](left: => X) = + @inline final def toRight[X](left: => X) = if (isEmpty) Left(left) else Right(this.get) /** Returns a [[scala.Right]] containing the given @@ -252,7 +251,7 @@ sealed abstract class Option[+A] extends Product with Serializable { * @param right the expression to evaluate and return if this is empty * @see toRight */ - def toLeft[X](right: => X) = + @inline final def toLeft[X](right: => X) = if (isEmpty) Right(right) else Left(this.get) } |