diff options
author | Simon Ochsenreither <simon@ochsenreither.de> | 2012-07-11 22:13:05 +0200 |
---|---|---|
committer | Simon Ochsenreither <simon@ochsenreither.de> | 2012-07-29 17:15:35 +0200 |
commit | 855f01b30b37ee8f07612d8e568eda5d408fd2df (patch) | |
tree | a89ffe26363609ef17e03864490fc246279fadfa | |
parent | bad93927836f302be3b973335a63bb5e69b2237c (diff) | |
download | scala-855f01b30b37ee8f07612d8e568eda5d408fd2df.tar.gz scala-855f01b30b37ee8f07612d8e568eda5d408fd2df.tar.bz2 scala-855f01b30b37ee8f07612d8e568eda5d408fd2df.zip |
SI-6064 Add method contains to Option.
The Option API more or less mirrors the Collection API,
but it seems that somehow this method has been forgotten.
Review: @axel22
-rw-r--r-- | src/library/scala/Option.scala | 9 | ||||
-rw-r--r-- | test/files/run/t6064.scala | 9 |
2 files changed, 18 insertions, 0 deletions
diff --git a/src/library/scala/Option.scala b/src/library/scala/Option.scala index c461b413d6..5953a51c78 100644 --- a/src/library/scala/Option.scala +++ b/src/library/scala/Option.scala @@ -209,6 +209,15 @@ sealed abstract class Option[+A] extends Product with Serializable { def withFilter(q: A => Boolean): WithFilter = new WithFilter(x => p(x) && q(x)) } + /** Tests whether the option contains a given value as an element. + * + * @param elem the element to test. + * @return `true` if the option has an element that is equal (as + * determined by `==`) to `elem`, `false` otherwise. + */ + final def contains[A1 >: A](elem: A1): Boolean = + !isEmpty && this.get == elem + /** Returns true if this option is nonempty '''and''' the predicate * $p returns true when applied to this $option's value. * Otherwise, returns false. diff --git a/test/files/run/t6064.scala b/test/files/run/t6064.scala new file mode 100644 index 0000000000..fc184dd92d --- /dev/null +++ b/test/files/run/t6064.scala @@ -0,0 +1,9 @@ +object Test extends App { + assert(Option(42) contains 42) + assert(Some(42) contains 42) + assert(Option(BigInt(42)) contains 42) + assert(Option(42) contains BigInt(42)) + assert(!(None contains 42)) + assert(Some(null) contains null) + assert(!(Option(null) contains null)) +}
\ No newline at end of file |