Skip to content

Instantly share code, notes, and snippets.

@olafurpg
Created April 25, 2017 15:44
Show Gist options
  • Save olafurpg/eb28b1da1556a79bc631f0e4e9ee7a04 to your computer and use it in GitHub Desktop.
Save olafurpg/eb28b1da1556a79bc631f0e4e9ee7a04 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: hlists.HCons[Int,hlists.HCons[String,hlists.HNil]] = hlists.HCons.apply[Int, hlists.HNil](42, hlists.HNil).merge[hlists.HCons[String,hlists.HNil]](hlists.HCons.apply[String, hlists.HNil]("foo", hlists.HNil));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment