-
-
Save ruippeixotog/6016144 to your computer and use it in GitHub Desktop.
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._ | |
trait TypeValue[A, R] { | |
def apply(): R | |
} | |
trait Size[A] extends TypeValue[A, Int] | |
implicit val shortSize = new Size[Short] { | |
def apply() = 2 | |
} | |
implicit val intSize = new Size[Int] { | |
def apply() = 4 | |
} | |
implicit val doubleSize = new Size[Double] { | |
def apply() = 8 | |
} | |
def sizeOf[A](implicit size: Size[A]) = size() | |
trait Name[A] extends TypeValue[A, String] | |
implicit val intName = new Name[Int] { | |
def apply() = "Int" | |
} | |
implicit val doubleName = new Name[Double] { | |
def apply() = "Double" | |
} | |
def nameOf[A](implicit name: Name[A]) = name() | |
sizeOf[Int] == 4 | |
nameOf[Double] == "Double" | |
type FixReturn[R] = ({ type λ[A] = TypeValue[A, R] })#λ[_] | |
trait HListTypeMapper[L <: HList, R, F[_] <: FixReturn[R]] { | |
def tmap(): List[R] | |
} | |
implicit def HNilTypeMapper[R, F[_] <: FixReturn[R]] = new HListTypeMapper[HNil, R, F] { | |
def tmap() = Nil | |
} | |
implicit def hlistTypeMapper[H, T <: HList, R, F[_] <: FixReturn[R]](implicit typeValue: F[H], tailMapper: HListTypeMapper[T, R, F]) = | |
new HListTypeMapper[H :: T, R, F] { | |
def tmap() = typeValue() :: tailMapper.tmap() | |
} | |
def tmap[L <: HList, R, F[_] <: FixReturn[R]](implicit mapper: HListTypeMapper[L, R, F]): List[R] = mapper.tmap() | |
tmap[Int :: Double :: Short :: Int :: HNil, Int, Size] == List(4, 8, 2, 4) | |
tmap[Int :: String :: Short :: Int :: HNil, Int, Size] // doesn't compile. there's no implicit Size[String] | |
tmap[Int :: Double :: Int :: Int :: HNil, String, Name] == List("Int", "Double", "Int", "Int") | |
tmap[Int :: Double :: Short :: Int :: HNil, String, Name] // doesn't compile. there's no implicit Name[Short] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment