java - Need to pass class at runtime generics scala -
i think header of question not clear try explain here.
lets have class a takes generic type t
class a[t] { // doing thing t }
now instantiating object in place of t pass class @ runtime like
class.forname("com.somthing.someclass") new a[com.somthing.someclass]() //but don't have com.somthing.someclass @ compile time
but how can use above class?
you can't that, there not t
@ runtime. type parameters "disappeas" during erasure , replaced java.lang.object
:
$ echo 'class a[t] { def f(t: t): t = t }' > test.scala $ scalac test.scala $ javap -c a.class compiled "test.scala" public class a<t> { public t f(t); code: 0: aload_1 1: areturn public a(); code: 0: aload_0 1: invokespecial #19 // method java/lang/object."<init>":()v 4: return }
Comments
Post a Comment