1Option(3) OCaml library Option(3)
2
3
4
6 Option - Option values.
7
9 Module Option
10
12 Module Option
13 : sig end
14
15
16 Option values.
17
18 Option values explicitly indicate the presence or absence of a value.
19
20
21 Since 4.08
22
23
24
25
26
27
28
29 Options
30 type 'a t = 'a option =
31 | None
32 | Some of 'a
33
34
35 The type for option values. Either None or a value Some v .
36
37
38
39 val none : 'a option
40
41
42 none is None .
43
44
45
46 val some : 'a -> 'a option
47
48
49 some v is Some v .
50
51
52
53 val value : 'a option -> default:'a -> 'a
54
55
56 value o ~default is v if o is Some v and default otherwise.
57
58
59
60 val get : 'a option -> 'a
61
62
63 get o is v if o is Some v and raise otherwise.
64
65
66 Raises Invalid_argument if o is None .
67
68
69
70 val bind : 'a option -> ('a -> 'b option) -> 'b option
71
72
73 bind o f is f v if o is Some v and None if o is None .
74
75
76
77 val join : 'a option option -> 'a option
78
79
80 join oo is Some v if oo is Some (Some v) and None otherwise.
81
82
83
84 val map : ('a -> 'b) -> 'a option -> 'b option
85
86
87 map f o is None if o is None and Some (f v) is o is Some v .
88
89
90
91 val fold : none:'a -> some:('b -> 'a) -> 'b option -> 'a
92
93
94 fold ~none ~some o is none if o is None and some v if o is Some v .
95
96
97
98 val iter : ('a -> unit) -> 'a option -> unit
99
100
101 iter f o is f v if o is Some v and () otherwise.
102
103
104
105
106 Predicates and comparisons
107 val is_none : 'a option -> bool
108
109
110 is_none o is true if and only if o is None .
111
112
113
114 val is_some : 'a option -> bool
115
116
117 is_some o is true if and only if o is Some o .
118
119
120
121 val equal : ('a -> 'a -> bool) -> 'a option -> 'a option -> bool
122
123
124 equal eq o0 o1 is true if and only if o0 and o1 are both None or if
125 they are Some v0 and Some v1 and eq v0 v1 is true .
126
127
128
129 val compare : ('a -> 'a -> int) -> 'a option -> 'a option -> int
130
131
132 compare cmp o0 o1 is a total order on options using cmp to compare val‐
133 ues wrapped by Some _ . None is smaller than Some _ values.
134
135
136
137
138 Converting
139 val to_result : none:'e -> 'a option -> ('a, 'e) result
140
141
142 to_result ~none o is Ok v if o is Some v and Error none otherwise.
143
144
145
146 val to_list : 'a option -> 'a list
147
148
149 to_list o is [] if o is None and [v] if o is Some v .
150
151
152
153 val to_seq : 'a option -> 'a Seq.t
154
155
156 to_seq o is o as a sequence. None is the empty sequence and Some v is
157 the singleton sequence containing v .
158
159
160
161
162
163OCamldoc 2023-01-23 Option(3)