Question

Is it possible to pass an HList as a function parameter?

This is what I have so far:

import shapeless._
import poly._

object id extends (Id ~> Id) {
  def apply[T](f: shapeless.Id[T]): shapeless.Id[T] = f
}

object ListHandler {
  def mapOverHlist[ListType <: HList](list: ListType) = list.map(id)
}

val x = "foo" :: "bar" :: 0xb33f :: HNil    
ListHandler.mapOverHlist(x)

Leads to this error:

could not find implicit value for parameter mapper:   
shapeless.ops.hlist.Mapper[ShapelessSpec.this.id.type,ListType]
Was it helpful?

Solution

Error text contains solution:

import shapeless.ops.hlist.Mapper

def mapOverHlist[L <: HList](list: L)(implicit m: Mapper[id.type, L]): m.Out =
  list.map(id)

Method map accepts an implicit parameter and you should provide such parameter (by adding the same parameter to declaration of your method).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top