Skip to content

Instantly share code, notes, and snippets.

@dwins
Created June 19, 2013 13:31
Show Gist options
  • Save dwins/5814349 to your computer and use it in GitHub Desktop.
Save dwins/5814349 to your computer and use it in GitHub Desktop.
object Predicates {
type Pred[-T] = (T => Boolean)
implicitly[Seq[Int] >:> List[Int]]
implicitly[Pred[Seq[Int]] <:< Pred[List[Int]]] // predicate of seq is a subtype of predicate of list
val hasLength3: Pred[Seq[Int]] = _.size == 3
hasLength3(Vector(1,2,3)) // we can pass any seq to hasLength3
hasLength3(List(1,2,3))
val listOfLength3: Pred[List[Int]] = hasLength3 // this ascription is safe because of the subtype relationship
listOfLength3(List(1,2,3))
// listOfLength3(Vector(1,2,3)) // fails to compile, listOfLength3 accepts only lists
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment