scala> def foo[U](t: Any) = t.asInstanceOf[U]
foo: [U](t: Any)U
scala> val s: String = foo("hi")
scala> val n = foo("hi")
java.lang.ClassCastException: java.lang.String cannot be cast to scala.runtime.Nothing$
at .<init>(<console>:6)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:9)
at RequestResult$.<clinit>(<console>)
at RequestResult$scala_repl_result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$18.apply(Interpreter.scala:981)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$18.apply(Interpreter.scala:981)
at scala.util.control.Exce...
Is there a way to write #foo so that it returns an Any if 'U' is not inferred or set explicitly to a "real" type?
No. The static type is U
. If this is inferred as Nothing
, the compiler won't allow a return value of type Any
.
You can improve the runtime error message:
def foo[U: Manifest](t: Any): U = if (implicitly[Manifest[U]] == manifest[Nothing])
error("type not provided")
else t.asInstanceOf[U]
Or follow the Arjan's suggestion.
The answer is that you need to always specify the generic type, with foo[String]("hi")
. Since the generic type U
doesn't appear in any of the parameters, it can't be inferred.
There's no way to make foo's generic parameter default to Any
when it can't be inferred (which is never -- it can never be inferred). If you were to redefine the function as
def foo[U](t:U)=t.asInstanceOf[U]
then the following call would not compile:
val s:Any="Hi"
foo[String](s)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With