Is it possible to accept arbitrary extractor as method argument
<p>I'd like to reuse existing extractors and compose them. <code>A =&gt; Option[B]</code> perfectly matches with <code>B =&gt; Option[C]</code>.</p>

<p>But I'm confused, how could I express such relation in code.</p>

<p>The obvious way is not available for a good reason:</p>

<pre><code>type Extractor[F,T] = {
  def unapply(from : F) : Option[T]
}

def bind[A,B,C](l : Extractor[A,B], r : Extractor[B,C]) = new {
  def unapply(from : A) : Option[C] = l.unapply(from).flatMap(r.unapply _)
}
</code></pre>

<p>scalac reply:</p>

<pre><code>Extractors.scala:7: error: Parameter type in structural refinement may not refer to an abstract type defined outside that refinement
    def unapply(from : A) : Option[C] = l.unapply(from).flatMap(r.unapply _)
                ^
Extractors.scala:3: error: Parameter type in structural refinement may not refer to an abstract type defined outside that refinement
    def unapply(from : F) : Option[T]
                ^
two errors found
</code></pre>

<p>Internet says it is expected behaviour due to type erasure.</p>

<p>Is it possible to rephrase the code so it would work properly?</p>