Skip to content

Instantly share code, notes, and snippets.

@blacktaxi
Created March 22, 2013 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blacktaxi/aa8a9190d8eff09b1847 to your computer and use it in GitHub Desktop.
Save blacktaxi/aa8a9190d8eff09b1847 to your computer and use it in GitHub Desktop.
Scala, pattern matching and Seq <-> List variance.
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