Skip to content

Instantly share code, notes, and snippets.

@jiamingd
Created June 12, 2017 17:16
Show Gist options
  • Save jiamingd/f7a48f5b2f9b9d30897812fa3a86c380 to your computer and use it in GitHub Desktop.
Save jiamingd/f7a48f5b2f9b9d30897812fa3a86c380 to your computer and use it in GitHub Desktop.
Typed return value can make the duplicate impl, the boilerplates gone, and more type safe. This is some drill and improvement on sample of "Scala in Depth"
object HLists {
trait HList {
type ConResult[A] <: HList
def ::[A](a: A): ConResult[A]
}
case class HCol[H, T](h: H, t: T) extends HList {
override type ConResult[A] = HCol[A, HCol[H, T]]
override def ::[A](a: A): ConResult[A] = {
HCol[A, HCol[H, T]](a, this)
}
}
case object HNil {
type ConResult[A] = HCol[A, HNil.type]
def ::[A](a: A): ConResult[A] = HCol(a, HNil)
}
}
import HLists._
val x = "ok"::Boolean::2L::HNil
@jiamingd
Copy link
Author

jiamingd commented Sep 8, 2017

Try Line 13 with: ConResult[A](a, this)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment