@@ -294,27 +294,27 @@ yield x * y</code></pre>
294
294
<tr >
295
295
<td ><span class =" label important " >Bad</span ><br >
296
296
<pre class =" highlight " ><code >val v42 = 42
297
- Some(3) match {
298
- case Some( v42) => ; println("42")
299
- case _ => ; println("Not 42")
297
+ 3 match {
298
+ case v42 => ; println("42")
299
+ case _ => ; println("Not 42")
300
300
}</code ></pre ></td >
301
301
<td >“v42” is interpreted as a name matching any Int value, and “42” is printed.</td >
302
302
</tr >
303
303
<tr >
304
304
<td ><span class =" label success " >Good</span ><br >
305
305
<pre class =" highlight " ><code >val v42 = 42
306
- Some(3) match {
307
- case Some( ` v42 ` ) => ; println("42")
308
- case _ => ; println("Not 42")
306
+ 3 match {
307
+ case ` v42 ` => ; println("42")
308
+ case _ => ; println("Not 42")
309
309
}</code ></pre ></td >
310
310
<td >”` v42 ` ” with backticks is interpreted as the existing val <pre class =" highlight " ><code >v42</code ></pre >, and “Not 42” is printed.</td >
311
311
</tr >
312
312
<tr >
313
313
<td ><span class =" label success " >Good</span ><br >
314
314
<pre class =" highlight " ><code >val UppercaseVal = 42
315
- Some(3) match {
316
- case Some( UppercaseVal) => ; println("42")
317
- case _ => ; println("Not 42")
315
+ 3 match {
316
+ case UppercaseVal => ; println("42")
317
+ case _ => ; println("Not 42")
318
318
}</code ></pre ></td >
319
319
<td ><pre class =" highlight " ><code >UppercaseVal</code ></pre > is treated as an existing val, rather than a new pattern variable, because it starts with an uppercase letter. Thus, the value contained within <pre class =" highlight " ><code >UppercaseVal</code ></pre > is checked against <pre class =" highlight " ><code >3</code ></pre >, and “Not 42” is printed.</td >
320
320
</tr >
@@ -397,6 +397,232 @@ Some(3) match {
397
397
<tr >
398
398
<td ><pre class =" highlight " ><code >x: String</code ></pre ></td >
399
399
<td >ascription (compile time)</td >
400
+ <td > </td >
401
+ </tr >
402
+ <tr >
403
+ <td ><span id =" options " class =" h2 " >options</span ></td >
404
+ <td > </td >
405
+ </tr >
406
+ <tr >
407
+ <td ><pre class =" highlight " ><code >Some(42)</code ></pre ></td >
408
+ <td >Construct a non empty optional value</td >
409
+ </tr >
410
+ <tr >
411
+ <td ><pre class =" highlight " ><code >None</code ></pre ></td >
412
+ <td >The singleton empty optional value</td >
413
+ </tr >
414
+ <tr >
415
+ <td ><pre class =" highlight " ><code >Option(null) == None
416
+ Option(obj.unsafeMethod)</code ></pre ></td >
417
+ <td >Null-safe optional value factory</td >
418
+ </tr >
419
+ <tr >
420
+ <td ><pre class =" highlight " ><code >val optStr: Option[ String] = None</code ></pre >
421
+ <em ><strong >same as</strong ></em >
422
+ <pre class =" highlight " ><code >val optStr = Option.empty[ String] </code ></pre ></td >
423
+ <td >Explicit type for empty optional value.<br /> Factory for empty optional value.</td >
424
+ </tr >
425
+ <tr >
426
+ <td ><pre class =" highlight " ><code >val name: Option[ String] =
427
+ request.getParameter("name")
428
+ val upper = name.map {
429
+ _ .trim
430
+ }
431
+ .filter {
432
+ _ .length != 0
433
+ }
434
+ .map {
435
+ _ .toUpperCase
436
+ }
437
+ println(upper.getOrElse(""))
438
+ </code ></pre ></td >
439
+ <td >Pipeline style</td >
440
+ </tr >
441
+ <tr >
442
+ <td ><pre class =" highlight " ><code >val upper = for {
443
+ name <- request.getParameter("name")
444
+ trimmed <- Some(name.trim)
445
+ if trimmed.length != 0
446
+ upper <- Some(trimmed.toUpperCase)
447
+ } yield upper
448
+ println(upper.getOrElse(""))</code ></pre ></td >
449
+ <td >for-comprehension syntax</td >
450
+ </tr >
451
+ <tr >
452
+ <td ><pre class =" highlight " ><code >option.map(f(_ ))</code ></pre >
453
+ <em ><strong >same as</strong ></em >
454
+ <pre class =" highlight " ><code >option match {
455
+ case Some(x) => ; Some(f(x))
456
+ case None => ; None
457
+ }</code ></pre ></td >
458
+ <td >Apply a function on the optional value</td >
459
+ </tr >
460
+ <tr >
461
+ <td ><pre class =" highlight " ><code >option.flatMap(f(_ ))</code ></pre >
462
+ <em ><strong >same as</strong ></em >
463
+ <pre class =" highlight " ><code >option match {
464
+ case Some(x) => ; f(x)
465
+ case None => ; None
466
+ }</code ></pre ></td >
467
+ <td >Same as map but function must return an optional value</td >
468
+ </tr >
469
+ <tr >
470
+ <td ><pre class =" highlight " ><code >optionOfOption.flatten</code ></pre >
471
+ <em ><strong >same as</strong ></em >
472
+ <pre class =" highlight " ><code >optionOfOption match {
473
+ case Some(Some(x)) => ; Some(x)
474
+ case _ => ; None
475
+ }</code ></pre ></td >
476
+ <td >Extract nested option</td >
477
+ </tr >
478
+ <tr >
479
+ <td ><pre class =" highlight " ><code >option.foreach(f(_ ))</code ></pre >
480
+ <em ><strong >same as</strong ></em >
481
+ <pre class =" highlight " ><code >option match {
482
+ case Some(x) => ; f(x)
483
+ case None => ; ()
484
+ }</code ></pre ></td >
485
+ <td >Apply a procedure on optional value</td >
486
+ </tr >
487
+ <tr >
488
+ <td ><pre class =" highlight " ><code >option.fold(y)(f(_ ))</code ></pre >
489
+ <em ><strong >same as</strong ></em >
490
+ <pre class =" highlight " ><code >option match {
491
+ case Some(x) => ; f(x)
492
+ case None => ; y
493
+ }</code ></pre ></td >
494
+ <td >Apply function on optional value, return default if empty</td >
495
+ </tr >
496
+ <tr >
497
+ <td ><pre class =" highlight " ><code >option.collect {
498
+ case x => ; ...
499
+ }</code ></pre >
500
+ <em ><strong >same as</strong ></em >
501
+ <pre class =" highlight " ><code >option match {
502
+ case Some(x)
503
+ if f.isDefinedAt(x) => ; ...
504
+ case Some(_ ) => ; None
505
+ case None => ; None
506
+ }</code ></pre ></td >
507
+ <td >Apply partial pattern match on optional value</td >
508
+ </tr >
509
+ <tr >
510
+ <td ><pre class =" highlight " ><code >option.isDefined</code ></pre >
511
+ <em ><strong >same as</strong ></em >
512
+ <pre class =" highlight " ><code >option match {
513
+ case Some(_ ) => ; true
514
+ case None => ; false
515
+ }</code ></pre ></td >
516
+ <td >True if not empty</td >
517
+ </tr >
518
+ <tr >
519
+ <td ><pre class =" highlight " ><code >option.isEmpty</code ></pre >
520
+ <em ><strong >same as</strong ></em >
521
+ <pre class =" highlight " ><code >option match {
522
+ case Some(_ ) => ; false
523
+ case None => ; true
524
+ }</code ></pre ></td >
525
+ <td >True if empty</td >
526
+ </tr >
527
+ <tr >
528
+ <td ><pre class =" highlight " ><code >option.nonEmpty</code ></pre >
529
+ <em ><strong >same as</strong ></em >
530
+ <pre class =" highlight " ><code >option match {
531
+ case Some(_ ) => ; true
532
+ case None => ; false
533
+ }</code ></pre ></td >
534
+ <td >True if not empty</td >
535
+ </tr >
536
+ <tr >
537
+ <td ><pre class =" highlight " ><code >option.size</code ></pre >
538
+ <em ><strong >same as</strong ></em >
539
+ <pre class =" highlight " ><code >option match {
540
+ case Some(_ ) => ; 1
541
+ case None => ; 0
542
+
543
+ </code></pre></td>
544
+ <td>Zero if empty, otherwise one</td>
545
+ </tr>
546
+ <tr>
547
+ <td><pre class="highlight"><code>option.orElse(Some(y))</code></pre>
548
+ <em><strong>same as</strong></em>
549
+ <pre class="highlight"><code>option match {
550
+ case Some(x) => ; Some(x)
551
+ case None => ; Some(y)
552
+ }</code ></pre ></td >
553
+ <td >Evaluate and return alternate optional value if empty</td >
554
+ </tr >
555
+ <tr >
556
+ <td ><pre class =" highlight " ><code >option.getOrElse(y)</code ></pre >
557
+ <em ><strong >same as</strong ></em >
558
+ <pre class =" highlight " ><code >option match {
559
+ case Some(x) => ; x
560
+ case None => ; y
561
+ }</code ></pre ></td >
562
+ <td >Evaluate and return default value if empty</td >
563
+ </tr >
564
+ <tr >
565
+ <td ><pre class =" highlight " ><code >option.get</code ></pre >
566
+ <em ><strong >same as</strong ></em >
567
+ <pre class =" highlight " ><code >option match {
568
+ case Some(x) => ; x
569
+ case None => ; throw new Exception
570
+ }</code ></pre ></td >
571
+ <td >Return value, throw exception if empty</td >
572
+ </tr >
573
+ <tr >
574
+ <td ><pre class =" highlight " ><code >option.orNull</code ></pre >
575
+ <em ><strong >same as</strong ></em >
576
+ <pre class =" highlight " ><code >option match {
577
+ case Some(x) => ; x
578
+ case None => ; null
579
+ }</code ></pre ></td >
580
+ <td >Return value, null if empty</td >
581
+ </tr >
582
+ <tr >
583
+ <td ><pre class =" highlight " ><code >option.filter(f)</code ></pre >
584
+ <em ><strong >same as</strong ></em >
585
+ <pre class =" highlight " ><code >option match {
586
+ case Some(x) if f(x) => ; Some(x)
587
+ case _ => ; None
588
+ }</code ></pre ></td >
589
+ <td >Optional value satisfies predicate</td >
590
+ </tr >
591
+ <tr >
592
+ <td ><pre class =" highlight " ><code >option.filterNot(f(_ ))</code ></pre >
593
+ <em ><strong >same as</strong ></em >
594
+ <pre class =" highlight " ><code >option match {
595
+ case Some(x) if !f(x) => ; Some(x)
596
+ case _ => ; None
597
+ }</code ></pre ></td >
598
+ <td >Optional value doesn't satisfy predicate</td >
599
+ </tr >
600
+ <tr >
601
+ <td ><pre class =" highlight " ><code >option.exists(f(_ ))</code ></pre >
602
+ <em ><strong >same as</strong ></em >
603
+ <pre class =" highlight " ><code >option match {
604
+ case Some(x) if (f(x)) => ; true
605
+ case _ => ; false
606
+ }</code ></pre ></td >
607
+ <td >Apply predicate on optional value or false if empty</td >
608
+ </tr >
609
+ <tr >
610
+ <td ><pre class =" highlight " ><code >option.forall(f(_ ))</code ></pre >
611
+ <em ><strong >same as</strong ></em >
612
+ <pre class =" highlight " ><code >option match {
613
+ case Some(x) if f(x) => ; true
614
+ case None => ; false
615
+ }</code ></pre ></td >
616
+ <td >Apply predicate on optional value or true if empty</td >
617
+ </tr >
618
+ <tr >
619
+ <td ><pre class =" highlight " ><code >option.contains(y)</code ></pre >
620
+ <em ><strong >same as</strong ></em >
621
+ <pre class =" highlight " ><code >option match {
622
+ case Some(x) => ; x == y
623
+ case None => ; false
624
+ }</code ></pre ></td >
625
+ <td >Checks if value equals optional value or false if empty</td >
400
626
</tr >
401
627
</tbody >
402
628
</table >
0 commit comments