Skip to content

Commit 0d8c108

Browse files
committed
Add doc to Option on pattern matches
1 parent b4c9844 commit 0d8c108

File tree

1 file changed

+146
-2
lines changed

1 file changed

+146
-2
lines changed

library/src/scala/Option.scala

Lines changed: 146 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,38 @@ sealed abstract class Option[+A] extends Product with Serializable {
135135
self =>
136136

137137
/** Returns true if the option is $none, false otherwise.
138+
*
139+
* This is equivalent to:
140+
* {{{
141+
* option match {
142+
* case Some(_) => false
143+
* case None => true
144+
* }
145+
* }}}
138146
*/
139147
def isEmpty: Boolean
140148

141149
/** Returns true if the option is an instance of $some, false otherwise.
150+
*
151+
* This is equivalent to:
152+
* {{{
153+
* option match {
154+
* case Some(_) => true
155+
* case None => false
156+
* }
157+
* }}}
142158
*/
143159
def isDefined: Boolean = !isEmpty
144160

145161
/** Returns the option's value.
162+
*
163+
* This is equivalent to:
164+
* {{{
165+
* option match {
166+
* case Some(x) => x
167+
* case None => throw new Exception
168+
* }
169+
* }}}
146170
* @note The option must be nonempty.
147171
* @throws java.util.NoSuchElementException if the option is empty.
148172
*/
@@ -151,15 +175,32 @@ sealed abstract class Option[+A] extends Product with Serializable {
151175
/** Returns the option's value if the option is nonempty, otherwise
152176
* return the result of evaluating `default`.
153177
*
178+
* This is equivalent to:
179+
* {{{
180+
* option match {
181+
* case Some(x) => x
182+
* case None => default
183+
* }
184+
* }}}
185+
*
154186
* @param default the default expression.
155187
*/
156188
@inline final def getOrElse[B >: A](default: => B): B =
157189
if (isEmpty) default else this.get
158190

159191
/** Returns the option's value if it is nonempty,
160192
* or `null` if it is empty.
193+
*
161194
* Although the use of null is discouraged, code written to use
162195
* $option must often interface with code that expects and returns nulls.
196+
*
197+
* This is equivalent to:
198+
* {{{
199+
* option match {
200+
* case Some(x) => x
201+
* case None => null
202+
* }
203+
* }}}
163204
* @example {{{
164205
* val initialText: Option[String] = getInitialText
165206
* val textField = new JComponent(initialText.orNull,20)
@@ -171,6 +212,13 @@ sealed abstract class Option[+A] extends Product with Serializable {
171212
* value if this $option is nonempty.
172213
* Otherwise return $none.
173214
*
215+
* This is equivalent to:
216+
* {{{
217+
* option match {
218+
* case Some(x) => Some(f(x))
219+
* case None => None
220+
* }
221+
* }}}
174222
* @note This is similar to `flatMap` except here,
175223
* $f does not need to wrap its result in an $option.
176224
*
@@ -185,8 +233,17 @@ sealed abstract class Option[+A] extends Product with Serializable {
185233
* value if the $option is nonempty. Otherwise, evaluates
186234
* expression `ifEmpty`.
187235
*
188-
* @note This is equivalent to `$option map f getOrElse ifEmpty`.
189-
*
236+
* This is equivalent to:
237+
* {{{
238+
* option match {
239+
* case Some(x) => f(x)
240+
* case None => ifEmpty
241+
* }
242+
* }}}
243+
* This is also equivalent to:
244+
* {{{
245+
* option map f getOrElse ifEmpty
246+
* }}}
190247
* @param ifEmpty the expression to evaluate if empty.
191248
* @param f the function to apply if nonempty.
192249
*/
@@ -199,6 +256,13 @@ sealed abstract class Option[+A] extends Product with Serializable {
199256
* Slightly different from `map` in that $f is expected to
200257
* return an $option (which could be $none).
201258
*
259+
* This is equivalent to:
260+
* {{{
261+
* option match {
262+
* case Some(x) => f(x)
263+
* case None => None
264+
* }
265+
* }}}
202266
* @param f the function to apply
203267
* @see map
204268
* @see foreach
@@ -212,6 +276,13 @@ sealed abstract class Option[+A] extends Product with Serializable {
212276
/** Returns this $option if it is nonempty '''and''' applying the predicate $p to
213277
* this $option's value returns true. Otherwise, return $none.
214278
*
279+
* This is equivalent to:
280+
* {{{
281+
* option match {
282+
* case Some(x) if p(x) => Some(x)
283+
* case _ => None
284+
* }
285+
* }}}
215286
* @param p the predicate used for testing.
216287
*/
217288
@inline final def filter(p: A => Boolean): Option[A] =
@@ -220,12 +291,27 @@ sealed abstract class Option[+A] extends Product with Serializable {
220291
/** Returns this $option if it is nonempty '''and''' applying the predicate $p to
221292
* this $option's value returns false. Otherwise, return $none.
222293
*
294+
* This is equivalent to:
295+
* {{{
296+
* option match {
297+
* case Some(x) if !p(x) => Some(x)
298+
* case _ => None
299+
* }
300+
* }}}
223301
* @param p the predicate used for testing.
224302
*/
225303
@inline final def filterNot(p: A => Boolean): Option[A] =
226304
if (isEmpty || !p(this.get)) this else None
227305

228306
/** Returns false if the option is $none, true otherwise.
307+
*
308+
* This is equivalent to:
309+
* {{{
310+
* option match {
311+
* case Some(_) => true
312+
* case None => false
313+
* }
314+
* }}}
229315
* @note Implemented here to avoid the implicit conversion to Iterable.
230316
*/
231317
final def nonEmpty = isDefined
@@ -248,6 +334,13 @@ sealed abstract class Option[+A] extends Product with Serializable {
248334

249335
/** Tests whether the option contains a given value as an element.
250336
*
337+
* This is equivalent to:
338+
* {{{
339+
* option match {
340+
* case Some(x) => x == elem
341+
* case None => false
342+
* }
343+
* }}}
251344
* @example {{{
252345
* // Returns true because Some instance contains string "something" which equals "something".
253346
* Some("something") contains "something"
@@ -270,6 +363,13 @@ sealed abstract class Option[+A] extends Product with Serializable {
270363
* $p returns true when applied to this $option's value.
271364
* Otherwise, returns false.
272365
*
366+
* This is equivalent to:
367+
* {{{
368+
* option match {
369+
* case Some(x) => p(x)
370+
* case None => false
371+
* }
372+
* }}}
273373
* @param p the predicate to test
274374
*/
275375
@inline final def exists(p: A => Boolean): Boolean =
@@ -278,13 +378,27 @@ sealed abstract class Option[+A] extends Product with Serializable {
278378
/** Returns true if this option is empty '''or''' the predicate
279379
* $p returns true when applied to this $option's value.
280380
*
381+
* This is equivalent to:
382+
* {{{
383+
* option match {
384+
* case Some(x) => p(x)
385+
* case None => true
386+
* }
387+
* }}}
281388
* @param p the predicate to test
282389
*/
283390
@inline final def forall(p: A => Boolean): Boolean = isEmpty || p(this.get)
284391

285392
/** Apply the given procedure $f to the option's value,
286393
* if it is nonempty. Otherwise, do nothing.
287394
*
395+
* This is equivalent to:
396+
* {{{
397+
* option match {
398+
* case Some(x) => f(x)
399+
* case None => ()
400+
* }
401+
* }}}
288402
* @param f the procedure to apply.
289403
* @see map
290404
* @see flatMap
@@ -319,6 +433,14 @@ sealed abstract class Option[+A] extends Product with Serializable {
319433

320434
/** Returns this $option if it is nonempty,
321435
* otherwise return the result of evaluating `alternative`.
436+
*
437+
* This is equivalent to:
438+
* {{{
439+
* option match {
440+
* case Some(x) => Some(x)
441+
* case None => alternative
442+
* }
443+
* }}}
322444
* @param alternative the alternative expression.
323445
*/
324446
@inline final def orElse[B >: A](alternative: => Option[B]): Option[B] =
@@ -332,6 +454,14 @@ sealed abstract class Option[+A] extends Product with Serializable {
332454

333455
/** Returns a singleton list containing the $option's value
334456
* if it is nonempty, or the empty list if the $option is empty.
457+
*
458+
* This is equivalent to:
459+
* {{{
460+
* option match {
461+
* case Some(x) => List(x)
462+
* case None => Nil
463+
* }
464+
* }}}
335465
*/
336466
def toList: List[A] =
337467
if (isEmpty) List() else new ::(this.get, Nil)
@@ -341,6 +471,13 @@ sealed abstract class Option[+A] extends Product with Serializable {
341471
* a [[scala.util.Right]] containing this $option's value if
342472
* this is nonempty.
343473
*
474+
* This is equivalent to:
475+
* {{{
476+
* option match {
477+
* case Some(x) => Right(x)
478+
* case None => Left(left)
479+
* }
480+
* }}}
344481
* @param left the expression to evaluate and return if this is empty
345482
* @see toLeft
346483
*/
@@ -352,6 +489,13 @@ sealed abstract class Option[+A] extends Product with Serializable {
352489
* a [[scala.util.Left]] containing this $option's value
353490
* if this $option is nonempty.
354491
*
492+
* This is equivalent to:
493+
* {{{
494+
* option match {
495+
* case Some(x) => Left(x)
496+
* case None => Right(right)
497+
* }
498+
* }}}
355499
* @param right the expression to evaluate and return if this is empty
356500
* @see toRight
357501
*/

0 commit comments

Comments
 (0)