Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Very good question.</p> <p>Let's check the source and see what's happening under the covers:</p> <pre><code>override def andThen[C](k: B =&gt; C): PartialFunction[A, C] = new AndThen[A, B, C] (this, k) </code></pre> <p>We can observe here that <code>andThen</code> <strong>doesn't even expect a Partial Function</strong>, any Function that transforms the result will do. Your code works, because: <code>trait PartialFunction[-A, +B] extends (A =&gt; B)</code>. This can actually be found in <a href="http://www.scala-lang.org/api/current/index.html#scala.PartialFunction" rel="nofollow">the documentation</a>:</p> <blockquote> <p><code>def andThen[C](k: (B) ⇒ C): PartialFunction[A, C]</code></p> <p>Composes this partial function with a transformation function that gets applied to results of this partial function.</p> <p><strong>C</strong> the result type of the transformation function.</p> <p><strong>k</strong> the transformation function</p> <p><strong>returns</strong> a partial function with the same domain as this partial function, which maps arguments <code>x</code> to <code>k(this(x))</code>.</p> </blockquote> <p>So there's currently no way to chain <code>PartialFunction</code>s in the way you would like, because as Robin said, it would require applying the function. Next to being computationally expensive <strong>it could also have side effects</strong>, which is a bigger problem.</p> <h2>Update</h2> <p>Whipped together an implementation you're looking for. <strong>Use it cautiously!</strong> As I already mentioned, if your code has side effects it will cause problems:</p> <pre><code>implicit class PartialFunctionExtension[-A, B](pf: PartialFunction[A, B]) { def andThenPf[C](pf2: PartialFunction[B, C]) = new PfAndThen(pf, pf2) class PfAndThen[+C](pf: PartialFunction[A, B], nextPf: PartialFunction[B, C]) extends PartialFunction[A, C] { def isDefinedAt(x: A) = pf.isDefinedAt(x) &amp;&amp; nextPf.isDefinedAt(pf.apply(x)) def apply(x: A): C = nextPf(pf(x)) } } </code></pre> <p>Trying it out:</p> <pre><code>deepTestLvl1.andThenPf(deepTestLvl2).isDefinedAt(Some(Some(3))) // false deepTestLvl1.andThenPf(deepTestLvl2).isDefinedAt(Some(None)) // true deepTestLvl1.andThenPf(deepTestLvl2).apply(Some(None)) // 3 </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.
    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