marstran marstran - 1 month ago 26
Scala Question

Unbound Wildcard Type

I was playing around in the Scala REPL when I got

error: unbound wildcard type
. I tried to declare this (useless) function:

def ignoreParam(n: _) = println("Ignored")


Why am I getting this error?

Is it possible to declare this function without introducing a named type variable? If so, how?

Answer

Scala doesn't infer types in arguments, types flow from declaration to use-site, so no, you can't write that. You could write it as def ignoreParam(n: Any) = println("Ignored") or def ignoreParam() = println("Ignored").

As it stands, your type signature doesn't really make sense. You could be expecting Scala to infer that n: Any but since Scala doesn't infer argument types, no winner. In Haskell, you could legally write ignoreParam a = "Ignored" because of its stronger type inference engine.

To get the closest approximation of what you want, you would write it as def ignoreParams[B](x: B) = println("Ignored") I suppose.

Comments