@@ -294,27 +294,27 @@ yield x * y</code></pre>
294294 <tr >
295295 <td ><span class =" label important " >Bad</span ><br >
296296 <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")
300300}</code ></pre ></td >
301301 <td >“v42” is interpreted as a name matching any Int value, and “42” is printed.</td >
302302 </tr >
303303 <tr >
304304 <td ><span class =" label success " >Good</span ><br >
305305 <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")
309309}</code ></pre ></td >
310310 <td >”` v42 ` ” with backticks is interpreted as the existing val <pre class =" highlight " ><code >v42</code ></pre >, and “Not 42” is printed.</td >
311311 </tr >
312312 <tr >
313313 <td ><span class =" label success " >Good</span ><br >
314314 <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")
318318}</code ></pre ></td >
319319 <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 >
320320 </tr >
@@ -397,6 +397,231 @@ Some(3) match {
397397 <tr >
398398 <td ><pre class =" highlight " ><code >x: String</code ></pre ></td >
399399 <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+ }</code ></pre ></td >
543+ <td >Zero if empty, otherwise one</td >
544+ </tr >
545+ <tr >
546+ <td ><pre class =" highlight " ><code >option.orElse(Some(y))</code ></pre >
547+ <em ><strong >same as</strong ></em >
548+ <pre class =" highlight " ><code >option match {
549+ case Some(x) => ; Some(x)
550+ case None => ; Some(y)
551+ }</code ></pre ></td >
552+ <td >Evaluate and return alternate optional value if empty</td >
553+ </tr >
554+ <tr >
555+ <td ><pre class =" highlight " ><code >option.getOrElse(y)</code ></pre >
556+ <em ><strong >same as</strong ></em >
557+ <pre class =" highlight " ><code >option match {
558+ case Some(x) => ; x
559+ case None => ; y
560+ }</code ></pre ></td >
561+ <td >Evaluate and return default value if empty</td >
562+ </tr >
563+ <tr >
564+ <td ><pre class =" highlight " ><code >option.get</code ></pre >
565+ <em ><strong >same as</strong ></em >
566+ <pre class =" highlight " ><code >option match {
567+ case Some(x) => ; x
568+ case None => ; throw new Exception
569+ }</code ></pre ></td >
570+ <td >Return value, throw exception if empty</td >
571+ </tr >
572+ <tr >
573+ <td ><pre class =" highlight " ><code >option.orNull</code ></pre >
574+ <em ><strong >same as</strong ></em >
575+ <pre class =" highlight " ><code >option match {
576+ case Some(x) => ; x
577+ case None => ; null
578+ }</code ></pre ></td >
579+ <td >Return value, null if empty</td >
580+ </tr >
581+ <tr >
582+ <td ><pre class =" highlight " ><code >option.filter(f)</code ></pre >
583+ <em ><strong >same as</strong ></em >
584+ <pre class =" highlight " ><code >option match {
585+ case Some(x) if f(x) => ; Some(x)
586+ case _ => ; None
587+ }</code ></pre ></td >
588+ <td >Optional value satisfies predicate</td >
589+ </tr >
590+ <tr >
591+ <td ><pre class =" highlight " ><code >option.filterNot(f(_ ))</code ></pre >
592+ <em ><strong >same as</strong ></em >
593+ <pre class =" highlight " ><code >option match {
594+ case Some(x) if !f(x) => ; Some(x)
595+ case _ => ; None
596+ }</code ></pre ></td >
597+ <td >Optional value doesn't satisfy predicate</td >
598+ </tr >
599+ <tr >
600+ <td ><pre class =" highlight " ><code >option.exists(f(_ ))</code ></pre >
601+ <em ><strong >same as</strong ></em >
602+ <pre class =" highlight " ><code >option match {
603+ case Some(x) if f(x) => ; true
604+ case _ => ; false
605+ }</code ></pre ></td >
606+ <td >Apply predicate on optional value or false if empty</td >
607+ </tr >
608+ <tr >
609+ <td ><pre class =" highlight " ><code >option.forall(f(_ ))</code ></pre >
610+ <em ><strong >same as</strong ></em >
611+ <pre class =" highlight " ><code >option match {
612+ case Some(x) if f(x) => ; true
613+ case None => ; false
614+ }</code ></pre ></td >
615+ <td >Apply predicate on optional value or true if empty</td >
616+ </tr >
617+ <tr >
618+ <td ><pre class =" highlight " ><code >option.contains(y)</code ></pre >
619+ <em ><strong >same as</strong ></em >
620+ <pre class =" highlight " ><code >option match {
621+ case Some(x) => ; x == y
622+ case None => ; false
623+ }</code ></pre ></td >
624+ <td >Checks if value equals optional value or false if empty</td >
400625 </tr >
401626 </tbody >
402627</table >
0 commit comments