scala – constructor pattern

These extra patterns mean that Scala patterns support deep matches. Such patterns not only check the top-level object supplied, but also check the contents of the object against further patterns.

Since the extra patterns canthemselvesbeconstructorpatterns,youcanusethemtocheckarbitrarily deep into an object.

For example, the pattern checks that the top-level object is a BinOp, that its third constructor parameter is a Number, and that the value field of that number is 0. This pattern is one line long yet checks three levels deep.

expr match { case BinOp(“+”, e, Number(0)) => println(“a deep match”) case _ => }