Skip to content

Instantly share code, notes, and snippets.

@mpollmeier
Created October 20, 2015 03:09
Show Gist options
  • Save mpollmeier/6c53e375d88a32016250 to your computer and use it in GitHub Desktop.
Save mpollmeier/6c53e375d88a32016250 to your computer and use it in GitHub Desktop.
shapeless derive mapped type
name := "shapeless-question"
scalaVersion := "2.11.7"
libraryDependencies += "com.chuusai" %% "shapeless" % "2.2.5"
import shapeless._
import shapeless.poly._
import shapeless.ops.hlist._
import shapeless.UnaryTCConstraint._
object Util {
case class Label[A](name: String, value: A)
object GetLabelName extends (Label ~> Const[String]#λ) {
def apply[A](label: Label[A]) = label.name
}
}
object Main {
import Util._
def bar(l: List[String]) = ???
def compiles = {
val names = "a" :: "b" :: HNil
bar(names.toList)
}
// A is an HList whose members are all Label[_]
def doesNotCompile[A <: HList : *->*[Label]#λ](labels: A)(
implicit ev1: Mapper[GetLabelName.type, A]) = {
// implicit ev1: Mapper[String, A]) = {
val names = labels map GetLabelName
// names is of type `ev1.Out` - I want it to be an HList of Strings
bar(names.toList)
// error: could not find implicit value for parameter toTraversableAux:
// shapeless.ops.hlist.ToTraversable.Aux[ev1.Out,List,Lub]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment