Skip to content

Instantly share code, notes, and snippets.

@larsrh
Last active July 22, 2016 07:50
Show Gist options
  • Select an option

  • Save larsrh/3e2d3789abc0a35b7dce7275b50ba816 to your computer and use it in GitHub Desktop.

Select an option

Save larsrh/3e2d3789abc0a35b7dce7275b50ba816 to your computer and use it in GitHub Desktop.
safe head/tail on lists using covariance
// Initial idea courtesy of @dwijnand
// Improved by @fthomas
sealed trait List[+T] {
def head: Option[T]
}
final case class Cons[T](h: T, t: List[T]) extends List[T] {
val head: Some[T] = Some(h)
}
final case object Nil extends List[Nothing] {
val head = None
}
// scala> Cons(1, Nil).head.x
// res0: Int = 1
// scala> Nil.head
// res1: None.type = None
@dwijnand

Copy link
Copy Markdown

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