How can get generic type in Javascript?


#1

Is possible to setup compiler for attach generic type to call function in Javascript? I need it for interactive deployment on Android via Rhino. I develop in Kotlin, then compile to Javascript and send to Rhino on Android phone where code is evaluated.

My problem is that code “Random.class in Kotlin written as javaClass<Random>()” is transformed to “javaClass()”, so I cannot get infomation from generic function about generic type. Solution in adding own parameter isn’t good due creating incompatibility versions (Kotlin->Java byte code and Kotlin->Javascript)

Could be possible add this generic type as last argument of calling function?

Thanks


#2

Emm, how you want get generic type from language that don't have it? Patching functions with special variables is very strange for me.

JVM and JS are incompatible platforms. Don’t try to write whole app for both. Just add your own class parameter.


#3

javaClass() doesn't work in JavaScript. It shouldn't even compile, but we are not there yet. Currently, we do not support any means of JS reflection.


#4

Thanks for answers, I forgot ask simple instead write for what I need it and how. Is there possibility to direct write Javascript in Kotlin file?

Something like:
fun getTypeClass<T>(){
  native("return " + T.toString() + “.class”)
}

or

fun getTypeClass<T>(){
  val type: T = T()
  native(“return” + type.getClass.toString() + “.class”)
}

which wll be generate for e.g. Random?

function getTypeClass(){
  return Random.class
}

Or is only way to implement toType function in external file and use it with modified first variation with eval and test how is set compiler?

fun getTypeClass<T>(){   var type: T = T();   var stype: String = toType(type);   eval("return" + stype + ".class"); }

To generate: function getTypeClass(){   var type = new Random();   var stype = toType(type);   eval("return" + stype + ".class"); }

Thank you