Skip to content

Instantly share code, notes, and snippets.

@olafurpg
Created April 25, 2017 15:44
Show Gist options
  • Save olafurpg/815ddd1fee10e385d17c8a9dbfd40c52 to your computer and use it in GitHub Desktop.
Save olafurpg/815ddd1fee10e385d17c8a9dbfd40c52 to your computer and use it in GitHub Desktop.
import scala.language.higherKinds
object hlists {
sealed abstract class HList {
type Merge[L <: HList] <: HList
def merge[L <: HList](l: L): Merge[L]
}
final case class HCons[H, T <: HList](head : H, tail : T) extends HList {
type Merge[L <: HList] = HCons[H, tail.Merge[L]]
def merge[L <: HList](l: L): Merge[L] = HCons(head, tail.merge(l))
}
sealed trait HNil extends HList {
type Merge[L <: HList] = L
def merge[L <: HList](l: L): Merge[L] = l
}
final val HNil: HNil = { case object HNil extends HNil; HNil }
}
object test {
import hlists._
val merged: HCons[Int,HCons[String,HNil]] = HCons(42, HNil) merge HCons("foo", HNil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment