user3663882 user3663882 - 10 months ago 102
Scala Question

Why is scala match.error thrown?

I'm trying to understand pattern-matching by writing a simple example. Here it is:

def main(args: Array[String]) = {
val tsm = new Test[Number]

class Test[T <: Any] {
def foo(t: Any): Unit = {
val f: PartialFunction[Any, Unit] = (_: Any) match {
case Val(s) => println("Val" + s)
case Sup(l) => println("Sup" + l)

class Val(val t: T)
class Sup(val l: Number)

object Val{
def apply(t: T): Val = new Val(t)
def unapply(arg: Val): Option[T] = Some(arg.t)
object Sup{
def unapply(arg: Sup): Option[Number] = Some(arg.l)

But the code throws:

Exception in thread "main" scala.MatchError: () (of class scala.runtime.BoxedUnit)
at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:253)
at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:251)
at com.test.Test$$anonfun$1.applyOrElse(Test.scala:7)
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36)
at com.test.App$.main(App.scala:6)
at com.test.App.main(App.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(

I don't quite understand it. I created an object of type
and passed it into
. I expected it printed


You define a function f: PartialFunction[Any, Unit] which takes an argument of type Any and then call it f() with no arguments. If you replace f() with f(t) (which is what I think you intended) everything works.