tags:

views:

82

answers:

3
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?

+1  A: 

I'm not sure whether this is what you mean, but can't you just use U as type for t instead of Any?


scala> def foo[U](t: U) = t.asInstanceOf[U]         
foo: [U](t: U)U

scala> foo("hi")
res0: java.lang.String = hi

scala> foo(1)   
res1: Int = 1

scala> val a:Any = "hi" 
a: Any = hi

scala> foo(a)
res2: Any = hi
Arjan Blokzijl
Makes sense to me.
abhin4v
But `foo[String](a)` won't compile.
Ken Bloom
Ken is right, that doesn't compile, so my answer won't work in this case
Arjan Blokzijl
+2  A: 

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) 
Ken Bloom
+5  A: 

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.

retronym
I would have expected that when there's a return value, Any would be inferred, rather than Nothing. I guess this is what happens when Scala sets high expectations ;-)
IttayD