Unbound Wildcard Type

marstran picture marstran · Jul 17, 2015 · Viewed 10k times · Source

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

tryx picture tryx · Jul 17, 2015

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.