@@ -252,27 +252,27 @@ breakable {
252
252
<tr >
253
253
<td ><span class =" label important " >Bad</span ><br >
254
254
<pre class =" highlight " ><code >val v42 = 42
255
- Some(3) match {
256
- case Some( v42) => println("42")
257
- case _ => println("Not 42")
255
+ 3 match {
256
+ case v42 => println("42")
257
+ case _ => println("Not 42")
258
258
}</code ></pre ></td >
259
259
<td >“v42” is interpreted as a name matching any Int value, and “42” is printed.</td >
260
260
</tr >
261
261
<tr >
262
262
<td ><span class =" label success " >Good</span ><br >
263
263
<pre class =" highlight " ><code >val v42 = 42
264
- Some(3) match {
265
- case Some( ` v42 ` ) => println("42")
266
- case _ => println("Not 42")
264
+ 3 match {
265
+ case ` v42 ` => println("42")
266
+ case _ => println("Not 42")
267
267
}</code ></pre ></td >
268
268
<td >”` v42 ` ” with backticks is interpreted as the existing val <code class =" highlighter-rouge " >v42</code >, and “Not 42” is printed.</td >
269
269
</tr >
270
270
<tr >
271
271
<td ><span class =" label success " >Good</span ><br >
272
272
<pre class =" highlight " ><code >val UppercaseVal = 42
273
- Some(3) match {
274
- case Some( UppercaseVal) => println("42")
275
- case _ => println("Not 42")
273
+ 3 match {
274
+ case UppercaseVal => println("42")
275
+ case _ => println("Not 42")
276
276
}</code ></pre ></td >
277
277
<td ><code class =" highlighter-rouge " >UppercaseVal</code > is treated as an existing val, rather than a new pattern variable, because it starts with an uppercase letter. Thus, the value contained within <code class =" highlighter-rouge " >UppercaseVal</code > is checked against <code class =" highlighter-rouge " >3</code >, and “Not 42” is printed.</td >
278
278
</tr >
@@ -353,6 +353,232 @@ Some(3) match {
353
353
<tr >
354
354
<td ><code class =" highlighter-rouge " >x: String</code ></td >
355
355
<td >ascription (compile time)</td >
356
+ <td > </td >
357
+ </tr >
358
+ <tr >
359
+ <td ><span id =" options " class =" h2 " >options</span ></td >
360
+ <td > </td >
361
+ </tr >
362
+ <tr >
363
+ <td ><pre class =" highlight " ><code >Some(42)</code ></pre ></td >
364
+ <td >Construct a non empty optional value</td >
365
+ </tr >
366
+ <tr >
367
+ <td ><pre class =" highlight " ><code >None</code ></pre ></td >
368
+ <td >The singleton empty optional value</td >
369
+ </tr >
370
+ <tr >
371
+ <td ><pre class =" highlight " ><code >Option(null) == None
372
+ Option(obj.unsafeMethod)</code ></pre ></td >
373
+ <td >Null-safe optional value factory</td >
374
+ </tr >
375
+ <tr >
376
+ <td ><pre class =" highlight " ><code >val optStr: Option[ String] = None</code ></pre >
377
+ <em ><strong >same as</strong ></em >
378
+ <pre class =" highlight " ><code >val optStr = Option.empty[ String] </code ></pre ></td >
379
+ <td >Explicit type for empty optional value.<br /> Factory for empty optional value.</td >
380
+ </tr >
381
+ <tr >
382
+ <td ><pre class =" highlight " ><code >val name: Option[ String] =
383
+ request.getParameter("name")
384
+ val upper = name.map {
385
+ _ .trim
386
+ }
387
+ .filter {
388
+ _ .length != 0
389
+ }
390
+ .map {
391
+ _ .toUpperCase
392
+ }
393
+ println(upper.getOrElse(""))
394
+ </code ></pre ></td >
395
+ <td >Pipeline style</td >
396
+ </tr >
397
+ <tr >
398
+ <td ><pre class =" highlight " ><code >val upper = for {
399
+ name <- request.getParameter("name")
400
+ trimmed <- Some(name.trim)
401
+ if trimmed.length != 0
402
+ upper <- Some(trimmed.toUpperCase)
403
+ } yield upper
404
+ println(upper.getOrElse(""))</code ></pre ></td >
405
+ <td >for-comprehension syntax</td >
406
+ </tr >
407
+ <tr >
408
+ <td ><pre class =" highlight " ><code >option.map(f(_ ))</code ></pre >
409
+ <em ><strong >same as</strong ></em >
410
+ <pre class =" highlight " ><code >option match {
411
+ case Some(x) => ; Some(f(x))
412
+ case None => ; None
413
+ }</code ></pre ></td >
414
+ <td >Apply a function on the optional value</td >
415
+ </tr >
416
+ <tr >
417
+ <td ><pre class =" highlight " ><code >option.flatMap(f(_ ))</code ></pre >
418
+ <em ><strong >same as</strong ></em >
419
+ <pre class =" highlight " ><code >option match {
420
+ case Some(x) => ; f(x)
421
+ case None => ; None
422
+ }</code ></pre ></td >
423
+ <td >Same as map but function must return an optional value</td >
424
+ </tr >
425
+ <tr >
426
+ <td ><pre class =" highlight " ><code >optionOfOption.flatten</code ></pre >
427
+ <em ><strong >same as</strong ></em >
428
+ <pre class =" highlight " ><code >optionOfOption match {
429
+ case Some(Some(x)) => ; Some(x)
430
+ case _ => ; None
431
+ }</code ></pre ></td >
432
+ <td >Extract nested option</td >
433
+ </tr >
434
+ <tr >
435
+ <td ><pre class =" highlight " ><code >option.foreach(f(_ ))</code ></pre >
436
+ <em ><strong >same as</strong ></em >
437
+ <pre class =" highlight " ><code >option match {
438
+ case Some(x) => ; f(x)
439
+ case None => ; ()
440
+ }</code ></pre ></td >
441
+ <td >Apply a procedure on optional value</td >
442
+ </tr >
443
+ <tr >
444
+ <td ><pre class =" highlight " ><code >option.fold(y)(f(_ ))</code ></pre >
445
+ <em ><strong >same as</strong ></em >
446
+ <pre class =" highlight " ><code >option match {
447
+ case Some(x) => ; f(x)
448
+ case None => ; y
449
+ }</code ></pre ></td >
450
+ <td >Apply function on optional value, return default if empty</td >
451
+ </tr >
452
+ <tr >
453
+ <td ><pre class =" highlight " ><code >option.collect {
454
+ case x => ; ...
455
+ }</code ></pre >
456
+ <em ><strong >same as</strong ></em >
457
+ <pre class =" highlight " ><code >option match {
458
+ case Some(x)
459
+ if f.isDefinedAt(x) => ; ...
460
+ case Some(_ ) => ; None
461
+ case None => ; None
462
+ }</code ></pre ></td >
463
+ <td >Apply partial pattern match on optional value</td >
464
+ </tr >
465
+ <tr >
466
+ <td ><pre class =" highlight " ><code >option.isDefined</code ></pre >
467
+ <em ><strong >same as</strong ></em >
468
+ <pre class =" highlight " ><code >option match {
469
+ case Some(_ ) => ; true
470
+ case None => ; false
471
+ }</code ></pre ></td >
472
+ <td >True if not empty</td >
473
+ </tr >
474
+ <tr >
475
+ <td ><pre class =" highlight " ><code >option.isEmpty</code ></pre >
476
+ <em ><strong >same as</strong ></em >
477
+ <pre class =" highlight " ><code >option match {
478
+ case Some(_ ) => ; false
479
+ case None => ; true
480
+ }</code ></pre ></td >
481
+ <td >True if empty</td >
482
+ </tr >
483
+ <tr >
484
+ <td ><pre class =" highlight " ><code >option.nonEmpty</code ></pre >
485
+ <em ><strong >same as</strong ></em >
486
+ <pre class =" highlight " ><code >option match {
487
+ case Some(_ ) => ; true
488
+ case None => ; false
489
+ }</code ></pre ></td >
490
+ <td >True if not empty</td >
491
+ </tr >
492
+ <tr >
493
+ <td ><pre class =" highlight " ><code >option.size</code ></pre >
494
+ <em ><strong >same as</strong ></em >
495
+ <pre class =" highlight " ><code >option match {
496
+ case Some(_ ) => ; 1
497
+ case None => ; 0
498
+
499
+ </code></pre></td>
500
+ <td>Zero if empty, otherwise one</td>
501
+ </tr>
502
+ <tr>
503
+ <td><pre class="highlight"><code>option.orElse(Some(y))</code></pre>
504
+ <em><strong>same as</strong></em>
505
+ <pre class="highlight"><code>option match {
506
+ case Some(x) => ; Some(x)
507
+ case None => ; Some(y)
508
+ }</code ></pre ></td >
509
+ <td >Evaluate and return alternate optional value if empty</td >
510
+ </tr >
511
+ <tr >
512
+ <td ><pre class =" highlight " ><code >option.getOrElse(y)</code ></pre >
513
+ <em ><strong >same as</strong ></em >
514
+ <pre class =" highlight " ><code >option match {
515
+ case Some(x) => ; x
516
+ case None => ; y
517
+ }</code ></pre ></td >
518
+ <td >Evaluate and return default value if empty</td >
519
+ </tr >
520
+ <tr >
521
+ <td ><pre class =" highlight " ><code >option.get</code ></pre >
522
+ <em ><strong >same as</strong ></em >
523
+ <pre class =" highlight " ><code >option match {
524
+ case Some(x) => ; x
525
+ case None => ; throw new Exception
526
+ }</code ></pre ></td >
527
+ <td >Return value, throw exception if empty</td >
528
+ </tr >
529
+ <tr >
530
+ <td ><pre class =" highlight " ><code >option.orNull</code ></pre >
531
+ <em ><strong >same as</strong ></em >
532
+ <pre class =" highlight " ><code >option match {
533
+ case Some(x) => ; x
534
+ case None => ; null
535
+ }</code ></pre ></td >
536
+ <td >Return value, null if empty</td >
537
+ </tr >
538
+ <tr >
539
+ <td ><pre class =" highlight " ><code >option.filter(f)</code ></pre >
540
+ <em ><strong >same as</strong ></em >
541
+ <pre class =" highlight " ><code >option match {
542
+ case Some(x) if f(x) => ; Some(x)
543
+ case _ => ; None
544
+ }</code ></pre ></td >
545
+ <td >Optional value satisfies predicate</td >
546
+ </tr >
547
+ <tr >
548
+ <td ><pre class =" highlight " ><code >option.filterNot(f(_ ))</code ></pre >
549
+ <em ><strong >same as</strong ></em >
550
+ <pre class =" highlight " ><code >option match {
551
+ case Some(x) if !f(x) => ; Some(x)
552
+ case _ => ; None
553
+ }</code ></pre ></td >
554
+ <td >Optional value doesn't satisfy predicate</td >
555
+ </tr >
556
+ <tr >
557
+ <td ><pre class =" highlight " ><code >option.exists(f(_ ))</code ></pre >
558
+ <em ><strong >same as</strong ></em >
559
+ <pre class =" highlight " ><code >option match {
560
+ case Some(x) if (f(x)) => ; true
561
+ case _ => ; false
562
+ }</code ></pre ></td >
563
+ <td >Apply predicate on optional value or false if empty</td >
564
+ </tr >
565
+ <tr >
566
+ <td ><pre class =" highlight " ><code >option.forall(f(_ ))</code ></pre >
567
+ <em ><strong >same as</strong ></em >
568
+ <pre class =" highlight " ><code >option match {
569
+ case Some(x) if f(x) => ; true
570
+ case None => ; false
571
+ }</code ></pre ></td >
572
+ <td >Apply predicate on optional value or true if empty</td >
573
+ </tr >
574
+ <tr >
575
+ <td ><pre class =" highlight " ><code >option.contains(y)</code ></pre >
576
+ <em ><strong >same as</strong ></em >
577
+ <pre class =" highlight " ><code >option match {
578
+ case Some(x) => ; x == y
579
+ case None => ; false
580
+ }</code ></pre ></td >
581
+ <td >Checks if value equals optional value or false if empty</td >
356
582
</tr >
357
583
</tbody >
358
584
</table >
0 commit comments