Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The solution is to use <code>Option.map</code> and <code>Option.flatMap</code>:</p> <pre><code>First.flatMap(_.second.flatMap(_.third.map(_.numberOfSmth))) </code></pre> <p>Or the equivalent (see the <strong>update</strong> at the end of this answer):</p> <pre><code>First flatMap(_.second) flatMap(_.third) map(_.numberOfSmth) </code></pre> <p>This returns an <code>Option[Int]</code> (provided that <code>numberOfSmth</code> returns an <code>Int</code>). If any of the options in the call chain is <code>None</code>, the result will be <code>None</code>, otherwise it will be <code>Some(count)</code> where <code>count</code> is the value returned by <code>numberOfSmth</code>.</p> <p>Of course this can get ugly very fast. For this reason scala supports <em>for comprehensions</em> as a syntactic sugar. The above can be rewritten as:</p> <pre><code>for { first &lt;- First second &lt;- first .second third &lt;- second.third } third.numberOfSmth </code></pre> <p>Which is arguably nicer (especially if you are not yet used to seeing <code>map</code>/<code>flatMap</code> everywhere, as will certainly be the case after a while using scala), and generates the exact same code under the hood.</p> <p>For more background, you may check this other question: <a href="https://stackoverflow.com/questions/1052476/can-someone-explain-scalas-yield">What is Scala&#39;s yield?</a></p> <p><strong>UPDATE</strong>: Thanks to Ben James for pointing out that flatMap is associative. In other words <code>x flatMap(y flatMap z)))</code> is the same as <code>x flatMap y flatMap z</code>. While the latter is usually not shorter, it has the advantage of avoiding any nesting, which is easier to follow.</p> <p>Here is some illustration in the REPL (the 4 styles are equivalent, with the first two using flatMap nesting, the other two using flat chains of flatMap):</p> <pre><code>scala&gt; val l = Some(1,Some(2,Some(3,"aze"))) l: Some[(Int, Some[(Int, Some[(Int, String)])])] = Some((1,Some((2,Some((3,aze)))))) scala&gt; l.flatMap(_._2.flatMap(_._2.map(_._2))) res22: Option[String] = Some(aze) scala&gt; l flatMap(_._2 flatMap(_._2 map(_._2))) res23: Option[String] = Some(aze) scala&gt; l flatMap(_._2) flatMap(_._2) map(_._2) res24: Option[String] = Some(aze) scala&gt; l.flatMap(_._2).flatMap(_._2).map(_._2) res25: Option[String] = Some(aze) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload