This post is not to say that Scala is a bad programming language it is just my opinion about how sometimes the pattern matching syntax can be ambiguous. I promise to keep it short.
Let’s take an example:
val data = List("First", "Second", "Third", "Fourth")
def check(data: List[String]) = data match {
case List(_, second, _)=> println(s"$second in a list of 3")
case List(_, second, _*) => println(s"$second in a list of 4 or more")
case _ => println("Unknown")
}
check(data) //Point: 1
check(data.take(3)) //Point: 2
check(data.take(2)) //Point: 3
check(data.take(1)) //Point: 4
/*
* Output:
* Second in a list of 4 or more
* Second in a list of 3
* Second in a list of 4 or more
* Unknown
*/
Based on whatever scarce documentation is around the language you would assume that the statement tagged above with Point: 3 would match against the default case i.e it would output “unknown” but actually it matches against the second case i.e its output is “Second in a list of 4 or more”.
Why would you think that it should match against the default case?
Because as human beings we (or at least I) assume that the pattern matching is saying that:
- If it is list of 3 (see the deconstruction syntax) take the second element and interpolate it with a string and print the resulting string.
- If it is a list of 3 or more (see the star in the deconstruction) take the second element and interpolate it with another string and print the resulting string.
- If it is anything else print “unknown”
Whereas semantically in Scala terms it translates to:
- If it is list of exactly 3 (see the deconstruction syntax) take the second element and interpolate it with a string and print the resulting string.
- If it is list of with at least 2 items and it is not a list with exactly 3 items (see the deconstruction syntax) take the second element and interpolate it with a string and print the resulting string.
- If it is anything else print “unknown”
Definition of ambiguity is (from the almighty Google):

Leave a Reply