Created
July 8, 2013 23:08
-
-
Save ramn/5953260 to your computer and use it in GitHub Desktop.
HList in Scala (v1 from Scala in depth)
This file contains hidden or 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
// HList implementation | |
sealed trait HList | |
final case class HCons[H, T <: HList](head: H, tail: T) extends HList { | |
def ::[T](v : T) = HCons(v,this) | |
override def toString = head + " :: " + tail | |
} | |
final class HNil extends HList { | |
def ::[T](v : T) = HCons(v,this) | |
override def toString = "Nil" | |
} | |
object HList { | |
type ::[H, T <: HList] = HCons[H,T] | |
val :: = HCons | |
val HNil = new HNil | |
} | |
// Used as such: | |
import HList._ | |
val x = "Hi" :: 5 :: false :: HNil | |
val one :: two :: three :: HNil = x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment