Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this PartialFunction defined but still crashes (correctly) after application in Scala?
    primarykey
    data
    text
    <p>I would like to try partial functions with a deep pattern matching use case. This initially (of course) didn't work after applying Some(Some(3)), but seemed defined instead :</p> <pre><code>def deepTest : PartialFunction [Option[Option[Int]], Int] = { case Some(v) =&gt; v match { case None =&gt; 3 } case None =&gt; 1 } </code></pre> <p>and I thought that by decoupling the nested pattern matching, things would be easier:</p> <pre><code>def deepTestLvl1 : PartialFunction [Option[Option[Int]], Option[Int]] = { case Some(v) =&gt; v case None =&gt; Some(1) } def deepTestLvl2 : PartialFunction [Option[Int], Int] = { case None =&gt; 3 } </code></pre> <p>but the result was the following:</p> <pre><code>scala&gt; (deepTestLvl1 andThen deepTestLvl2) isDefinedAt(Some(Some(3))) res24: Boolean = true </code></pre> <p>and after applying:</p> <pre><code>scala&gt; (deepTestLvl1 andThen deepTestLvl2) (Some(Some(3))) scala.MatchError: Some(3) (of class scala.Some) at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:248) at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:246) at $anonfun$deepTestLvl2$1.applyOrElse(&lt;console&gt;:7) at $anonfun$deepTestLvl2$1.applyOrElse(&lt;console&gt;:7) .... at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:83) at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96) at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105) at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala) </code></pre> <p>Am I doing something incorrectly? Shouldn't the isDefinedAt be called twice when I composed sequentially deepTestLvl{1,2} and give me the correct answer?</p>
    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.
 

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