Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

avoiding cast to Nothing in generic method

Tags:

generics

scala

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?

like image 228
IttayD Avatar asked Jul 01 '10 11:07

IttayD


2 Answers

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.

like image 51
retronym Avatar answered Sep 18 '22 22:09

retronym


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) 
like image 35
Ken Bloom Avatar answered Sep 17 '22 22:09

Ken Bloom