Don Branson
Don Branson

Reputation: 13709

What's the difference between these two Scala patterns?

In Scala 2.10.6, what's the difference between an object that will not match

case Array((_, workItem: WorkItem)) => 

but will match

case things: Array[_] => things.foreach { case (_, workItem: WorkItem) => 

I'm seeing this occur. Some objects will match the first one, others will not match the first form but will match the second. Without the second form, a MatchError is thrown:

scala.MatchError: [Lscala.Tuple2;@43d82d2a (of class [Lscala.Tuple2;)

Upvotes: 0

Views: 52

Answers (1)

puhlen
puhlen

Reputation: 8529

The first is matching an Array[(_, WorkiItem)] with exactly one element, the second matches any Array and will not throw MatchError in the foreach if all of its elements are (_, WorkItem)

Upvotes: 3

Related Questions