title: Is there a nicer way of lifting a PartialFunction in Scala?
body: <p>I occasionally come across the following pattern, where I essentially have a <code>PartialFunction[SomeType,AnotherType]</code>, and want to treat it as a <code>Function[SomeType,Option[AnotherType]</code>, eg:</p>
  
  <pre><code>def f(s:SomeType):Option[AnotherType] = s match {
    case s1:SubType1 =&gt; Some(AnotherType(s1.whatever))
    case s2:SubType2 =&gt; Some(AnotherType(s2.whatever))
    case _ =&gt; None
  }
  </code></pre>
  
  <p>Is there a way to write the above function in a way that avoids the default case and wrapping the result in <code>Some</code> where it's defined? The best I've come up with so far is this:</p>
  
  <pre><code>def f(s:SomeType):Option[AnotherType] = pf.lift(s)
  def pf:PartialFunction[SomeType,AnotherType] = {
    case s1:SubType1 =&gt; AnotherType(s1.whatever)
    case s2:SubType2 =&gt; AnotherType(s2.whatever)
  }
  </code></pre>
  
  <p>Is there a way to do it without defining an intermediate function? I've already tried various things along the lines of the following, but haven't got anything to compile yet:</p>
  
  <pre><code>def f:Function[SomeType,Option[AnotherType]] = {
    case s1:SubType1 =&gt; AnotherType(s1.whatever)
    case s2:SubType2 =&gt; AnotherType(s2.whatever)
  }.lift
  </code></pre>