andylamax
12/10/2023, 2:11 AMJsAny
instead of just using the already existing Any
?
• Why do we need to use it as an upper bound to any external generic function? (i.e.)
external Box<T> // fails with 'Type Any? cannot be used in type parameter upper bound'
external Box<T: JsAny?> // passes
But this now makes my common classes unusable. see code below
class Person(val name: String)
val boxed: Box<Person> // fails with 'Exepcted JsAny found Person'
If I extend JsAny
(which is dirty altogether)
class Person(val name: String) : JsAny // fails with 'Non-external type extends external type JsAny'
• If we must introduce JsAny
could we at least make it similar to Any
? coz as of now, JsAny
is an external interface while Any
is an open class, which does prohibit expect/actual all together.
Clearly I am missing something here.
How do I go about defining external generics and using it from classes defined in my common code??andylamax
12/10/2023, 2:25 AMtoJsReference()
am I heading in the right direction?andylamax
12/10/2023, 2:29 AMSvyatoslav Kuzmich [JB]
12/10/2023, 6:33 AMJsAny
instead of just using the already existing Any
?
Existing Any
static type provides too little information for compiler to convert values between JS and Kotlin. For example, if you receive JS number
, compiler won’t know if you want Double
or Int
to be put into your Any
JsAny
is added as a general low-level way to reference any JS value. You can use it as a “handle” to JS world in Kotlin. Compiler doesn’t convert JsAny
values to Kotlin automatically, so it can’t choose the wrong thing. You can model a richer JS type hierarchy with external types, like external class C : JsAny { ... }
, and use JsAny
in type parameters upper-bounds.
> Why do we need to use it as an upper bound to any external generic function?
We are thinking on how to relax this restriction. In general, type parameters in Kotlin are erased to its upper bound, so if we fully support <T>
, we should support Any?
as a default upper bound.
> I am stumbling across toJsReference()
am I heading in the right direction?
JsReference is another low-level primitive, but used in opposite direction. It can provide JS with a reference to any Kotlin value. But, like JsAny
, it is not converted, an you can’t do much with it directly in JS, but you can export Kotlin “helper methods” that accept JsReference
as “this” parameter.
All of this provide a very low-level, but functionally complete foundation to do any interop. The goal is for it to be useful in common code, and we need to add a lot more things in future versions!Robert Jaros
12/10/2023, 6:40 AMandylamax
12/10/2023, 7:45 AMJsReference is another low-level primitive, but used in opposite direction. It can provide JS with a reference to any Kotlin valueIf it provide's JS with a reference to any kotlin value, wouldn't it make more sense if the method was called,
toKotlinReference()
coz it provides a kotlin reference and not a js reference?Svyatoslav Kuzmich [JB]
12/10/2023, 8:06 AMtoKotlinReference
, you can say that Any
is already a Kotlin reference 🙂 . Suggestions for a better name for toJsValueRepresentingOpaqueKotlinReference
are more than welcome!Svyatoslav Kuzmich [JB]
12/10/2023, 8:09 AMtoJsHandle
, but it was also confusing for some people.andylamax
12/10/2023, 9:08 AMandylamax
12/10/2023, 9:09 AMSvyatoslav Kuzmich [JB]
12/10/2023, 9:20 AMJsReference<ToKotlinType>
andylamax
12/10/2023, 9:38 AMandylamax
12/10/2023, 9:39 AM