Skip to content

Instantly share code, notes, and snippets.

@arschles
Created January 11, 2013 22:15
Show Gist options
  • Save arschles/4514433 to your computer and use it in GitHub Desktop.
Save arschles/4514433 to your computer and use it in GitHub Desktop.
trait PolyFunc[Out] {
def apply[In]: In => Out
}
sealed trait HList {
}
//the end of an HList
sealed class HNil extends HList {
//the operator to prepend to an HNil
def ::[T](v : T) = HCons(v, this)
override def toString = "HNil"
}
//the current HList. has an operator to prepend an element to it
final case class HCons[H, T <: HList](head : H, tail : T) extends HList {
//the operator to prepend v to this HCons
def ::[T](v : T) = HCons(v, this)
override def toString = "%s :: %s".format(head.toString, tail.toString)
}
// aliases for building HList types and for pattern matching
object HList {
//the type to go between HList elements
type ::[H, T <: HList] = HCons[H, T]
//the operator to concatenate a value onto an HList
val :: = HCons
//the value to represent the end of an HList
val HNil = new HNil
}
import HList._
//x and y are equivalent
val x = HCons("a", HCons("b", HCons(1, HNil)))
val y = "a" :: "b" :: 1 :: HNil
println("x = %s".format(x))
println("y = %s".format(y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment