title: Can Map be performed on a Scala HList
body: <p>I have done a few implementations of HList now.  One based on Daniel Spiewak's High Wizardry in the Land of Scala talk and another based on a post in Apocalisp blog.  The goal was to have a heterogenous list of which is not heterogenous in the primary type but rather the higher kind.  For example:</p>
  
  <pre><code>val requests = Request[String] :: Request[Int] :: HNil
  </code></pre>
  
  <p>I would be able to do a map across the list to perform the request and result in a heterogenous list of the higher kind.  So:</p>
  
  <pre><code>requests.map(execute)
  </code></pre>
  
  <p>should equal</p>
  
  <pre><code>String :: Int :: HNil
  </code></pre>
  
  <p>Sadly all my attempts have resulted in an HList of Any.  Here is the code from a recent attempt:</p>
  
  <pre><code>class Request[+Out](o:Out) {
    type O = Out
  
    def v:O = o
  }
  
  object HList {
    trait Func[-Elem,Out] {
      type Apply[E &lt;: Elem] &lt;: Out
      def apply[N &lt;: Elem](e:N):Apply[N]
    }
    sealed trait HList[Base] {
      type Head &lt;: Base
      type Tail &lt;: HList[Base]
      type Map[Out,F &lt;: Func[Base,Out]] &lt;: HList[Out]
      def head:Head
      def tail:Tail
  
      def ::[A &lt;: Base](a:A):HList[Base]
      def map[Out,F &lt;: Func[Base,Out]](f:F):Map[Out,F]
    }
  
    case class HNil[Base]() extends HList[Base] {
      type Head = Nothing
      type Tail = Nothing
      type Map[Out,F &lt;: Func[Base,Out]] = HNil[Out]
  
      def head = error("Head of an empty HList")
      def tail = error("Head of an empty HList")
  
      def ::[A &lt;: Base](a:A) = HCons(a,this)
      def map[Out,F &lt;: Func[Base,Out]](f:F) = new HNil[Out]
    }
  
    case class HCons[Base,A &lt;: Base,B &lt;: HList[Base]](head: A, tail: B) extends HList[Base] {
      type Head = A
      type Tail = B    
      type Map[Out,F &lt;: Func[Base,Out]] = HCons[Out,F#Apply[Head],Tail#Map[Out,F]]
  
      def ::[C &lt;: Base](c:C) = HCons(c,this)
      def map[Out,F &lt;: Func[Base,Out]](f:F) =
        HCons(f(head),tail.map(f))
    }
  
    val :: = HCons 
  }
  
  object Test extends Application {
    import HList._
  
    val HNil = new HNil[Request[_]]
  
    val list = new Request[Int](1) :: new Request[String]("1") :: HNil
  
    val (a :: b :: HNil) = list
    val y:Request[String] = b
  
    val results = list.map[Any,Unwrap.type](Unwrap)
  
    val i:Int = results.head
  }
  
  import HList._
  object Unwrap extends Func[Request[Any],Any] {
    type Apply[I &lt;: Request[Any]] = I#O
    def apply[N &lt;: Request[Any]](e:N) = null.asInstanceOf[Apply[N]]
  }
  </code></pre>
  
  <p>The other attempt was based on the Apocalisp version which uses fold to create a new HList and again it resulted in a HList of Any types.  Any tips would be appreciated.</p>