Unbound wildcard type
I was playing with the Scala REPL when I got it 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 variable of type named? If so, how?
source to share
Scala does not infer types in arguments, types are passed from declaration to site-to-site, so no, you cannot write that. You can write it as def ignoreParam(n: Any) = println("Ignored")
or def ignoreParam() = println("Ignored")
.
In a way, your type signature doesn't make sense. You might expect Scala to infer that n: Any
, but since Scala does not infer the argument types, there is no winner. In Haskell, you can legally write ignoreParam a = "Ignored"
because of the stronger inference mechanism.
To get the closest approximation to what you want, you should write it down as def ignoreParams[B](x: B) = println("Ignored")
I suppose.
source to share