title: scala two options defined, return first, else return second
body: <p>If I have optionA, and optionB, how can I return optionA if both are defined and optionB is both are not defined.</p>
  
  <p>Basically, I am trying to rewrite this</p>
  
  <pre><code>val result: Option[Long] = 
    if(optionA.isDefined &amp;&amp; optionB.isDefined)
       optionA
    else
       optionB
  </code></pre>
  
  <p>No, optionA.orElse(optionB) is not the same and broke our test cases.  BOTH options must be defined and we want to use optionA.  IF optionA is defined and optionB is not defined, we want None.</p>
  
  <p>Someone marked my question as a duplicate which it is not.  I was having trouble but finally stumbled upon the answer....</p>
  
  <p>ok, I think I got it and I definitely think it is less human readable</p>
  
  <pre><code>optionA.flatMap { aId =&gt;
      optionB.map(bId =&gt; bId).orElse(Some(aId))
  }
  </code></pre>
  
  <p>for added clarity.  our truth table is thus</p>
  
  <pre><code>optionA.isDefined  optionB.isDefined   resultNeeded
      None               None               None
      Some(a)            None               None
      None               Some(b)            Some(b)
      Some(a)            Some(b)            Some(a)
  </code></pre>
  
  <p>thanks</p>