“unbound wildcard type” errors in Scala
In Scala 2.8.1, I ran into problems while compiling this:
object Test
{
def log(item: _) : Unit =
println("you logged " + item)
def main(args: Array[String]) : Unit =
{
log(42)
}
}The error message wasn’t not helping that much at the first glance:
/home/niels/tmp/test.scala:3: error: unbound wildcard type
def log(item: _) : Unit =
^To fix it, change the prototype of the log function like this:
def log(item: Any) : Unit =
There is a pretty good overview of the Scala type hierarchy on the Scala homepage. In short, Scala Objects are subtypes of AnyRef. Primitive types (Int, Boolean, …) are subtypes of “AnyVal”. Both “AnyRef” and “AnyVal” are subtypes of “Any”, and substituting “Any” with “_” in prototypes and templates is not allowed in Scala 2.8.