Created
March 22, 2013 23:03
-
-
Save blacktaxi/aa8a9190d8eff09b1847 to your computer and use it in GitHub Desktop.
Scala, pattern matching and Seq <-> List variance.
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
scala> def withList[T](x: List[T]) = x match { case Seq(1, 2, 3) => 1 } | |
withList: [T](x: List[T])Int | |
scala> withList(List(1, 2, 3)) | |
res6: Int = 1 | |
scala> withList(Seq(1, 2, 3)) | |
<console>:9: error: type mismatch; | |
found : Seq[Int] | |
required: List[?] | |
withList(Seq(1, 2, 3)) | |
^ | |
scala> def withSeq[T](x: Seq[T]) = x match { case List(1, 2, 3) => 1 } | |
<console>:7: warning: match is not exhaustive! | |
missing combination * Nil * * | |
def withSeq[T](x: Seq[T]) = x match { case List(1, 2, 3) => 1 } | |
^ | |
warning: there were 1 unchecked warnings; re-run with -unchecked for details | |
withSeq: [T](x: Seq[T])Int | |
scala> withSeq(List(1, 2, 3)) | |
res8: Int = 1 | |
scala> withSeq(Seq(1, 2, 3)) | |
res9: Int = 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment