title: Basic Metaprogramming techniques in Scala
body: <p>Whats the neatest way of specifying the following concepts in scala ? The concept represent an interface to a generic extractor function that that has specifies the class that is being extracted at compile time.</p>
  
  <p>Definition of Contruct</p>
  
  <pre><code>import org.jsoup.Jsoup
  import org.jsoup.nodes.Document
  import scala.collection.JavaConversions._
  
  trait ExtractorBase {
    type ExtractedType
    type RetType = Tuple2[Option[Seq[ExtractedType]],Option[Seq[String]]]
    type ExtractorFunction = (Document) =&gt; RetType
    def extractor : ExtractorFunction
  }
  class Extractor[T] (extractor_in: Any) extends ExtractorBase {
    type ExtractedType = T
    val extractor : ExtractorFunction = extractor_in
  }
  </code></pre>
  
  <p>Instance of Contruct</p>
  
  <pre><code>//type TFunc = (Document) =&gt; Tuple2[Option[Seq[Int]],Option[Seq[String]]]
  val ex = new Extractor[Int]( (x: Document)=&gt; {
      (Some(Seq(1)),Some(Seq("hassan")))
  })
  </code></pre>
  
  <p>At the moments the extractor function isn't aliasing correctly either :</p>
  
  <pre><code>[error] /Users/hassan/code/scala/extractor/hon.scala:14: type mismatch;
  [error]  found   : Any
  [error]  required: org.jsoup.nodes.Document =&gt; (Option[Seq[Extractor.this.ExtractedType]], Option[Seq[String]])
  [error]     (which expands to)  org.jsoup.nodes.Document =&gt; (Option[Seq[T]], Option[Seq[String]])
  [error]   val extractor : ExtractorFunction = extractor_in
  </code></pre>
  
  <ul>
  <li><p>How do I reference generic trait data outside of the definition of the classes extending the traits ? At compile time, and runtime. For example of compile time i'd rather have :</p>
  
  <p>trait ExtractorBase[T] {
    type ExtractedType = T
    type RetType = Tuple2[Option[Seq[ExtractedType]],Option[Seq[String]]]
    type ExtractorFunction = (Document) => RetType
    def extractor : ExtractorFunction
  }</p></li>
  </ul>
  
  <p>and then </p>
  
  <p><code>class Extractor[T] (extractor_in: ExtractorBase[T].ExtractorFunction) extends ExtractorBase</code> </p>
  
  <p>Kind of similar to C++ traits.</p>
  
  <ul>
  <li><p>At runtime I wouldn't mind getting at the type information in the trait either.</p></li>
  <li><p>there is probably a more idiomatic way of creating instances of the trait, I don't want to have to go through the <code>Extractor[T]</code> class. Is there a way to do it with a companion object of the trait ? </p></li>
  </ul>
  
  <p><strong>edit</strong></p>
  
  <p>Ankur's answer led me to the right path. I was missing the # operator from my repertoire. times to dress this up.</p>
  
  <pre><code>trait ExtractorBase[T] {
    type ExtractedType = T
    type RetType = Tuple2[Option[Seq[ExtractedType]],Option[Seq[String]]]
    type ExtractorFunction = (Document) =&gt; RetType
    def extractor : ExtractorFunction
  }
  class Extractor[T] (extractor_in: ExtractorBase[T]#ExtractorFunction) extends ExtractorBase[T] {
    def extractor : ExtractorBase[T]#ExtractorFunction = extractor_in
  }
  </code></pre>
  
  <p>Leading to : </p>
  
  <pre><code>val ex = new Extractor[Int]( (x: Document)=&gt; {
        (Some(Seq(1)),Some(Seq("hassan")))
  })
  </code></pre>