Created
June 19, 2013 13:31
-
-
Save dwins/5814349 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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