Created
October 20, 2015 03:09
-
-
Save mpollmeier/6c53e375d88a32016250 to your computer and use it in GitHub Desktop.
shapeless derive mapped type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name := "shapeless-question" | |
scalaVersion := "2.11.7" | |
libraryDependencies += "com.chuusai" %% "shapeless" % "2.2.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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