blob: ee0e32e636124a9757e6f42ca6b816b74fe49b4b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2002-2006, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id$
package scala;
import Predef._
/** This class represents optional values. Instances of <code>Option</code>
* are either instances of case class <code>Some</code> or it is case
* object <code>None</code>.
*
* @author Martin Odersky
* @author Matthias Zenger
* @version 1.0, 16/07/2003
*/
sealed abstract class Option[+A] extends Iterable[A] with Product {
def isEmpty: Boolean = this match {
case None => true
case _ => false
}
/**
* @throws Predef.NoSuchElementException
*/
def get: A = this match {
case None => throw new NoSuchElementException("None.get")
case Some(x) => x
}
def get[B >: A](default: B): B = this match {
case None => default
case Some(x) => x
}
def map[B](f: A => B): Option[B] = this match {
case None => None
case Some(x) => Some(f(x))
}
def flatMap[B](f: A => Option[B]): Option[B] = this match {
case None => None
case Some(x) => f(x)
}
def filter(p: A => Boolean): Option[A] = this match {
case None => None
case Some(x) => if (p(x)) Some(x) else None
}
override def foreach(f: A => Unit): Unit = this match {
case None => ()
case Some(x) => f(x)
}
def elements: Iterator[A] = this match {
case None => Iterator.empty
case Some(x) => Iterator.fromValues(x)
}
override def toList: List[A] = this match {
case None => List()
case Some(x) => List(x)
}
}
/** Class <code>Some[A]</code> represents existing values of type
* <code>A</code>.
*
* @author Martin Odersky
* @version 1.0, 16/07/2003
*/
final case class Some[+A](x: A) extends Option[A]
/** This case object represents non-existent values.
*
* @author Martin Odersky
* @version 1.0, 16/07/2003
*/
case object None extends Option[Nothing];
|