Hello! I am working with Javascript library which ...
# javascript
r
Hello! I am working with Javascript library which uses regular functions. When I tried to override functions in Kotlin, then JS treated those as "arrow functions" and could not use "this". This is example from this Javascript library: https://github.com/mapbox/mapbox-gl-draw/blob/main/docs/MODES.md#writing-custom-modes How can I replicate regular function in Kotlin?
a
Could you please share a snippet of Kotlin code?
t
Looks like you need special function types, which will give you access to
this
of function
r
Yes, I could not replicate this error because we moved to plain JS for this feature, but I am sure that error was related to acces to "this". Is there any way to access "this" in same way as regular function in JS?
t
We can provide such types in Kotlin Wrappers, could you please create issue
r
I will, thanks
t
Is there any way to access "this" in same way as regular function in JS?
Yes, with adapter
r
So I have to create issue for this adapter, or is this something I can code on my own right now?
t
I will share draft in issue, which you can copy in your project
Issue "Function types with
this
access"
r
t
@Rafał Kuźmiński We added required types and adapters. Release is planned for next week. While it's not released you can copy required files in your project.
Released in
pre.755
🎉
🎉 1
r
Does pre.755 require Kotlin 2.0? After update I have "ClassCast" exception. I use wrappers-bom to make sure that all dependencies are correct. I believe it crashes while loading modules. When I come back to pre.733 project works just fine.
t
pre.756
requires K2
r
Oh, well so it should work. Anyway, for now I'll just copy whole js.function package to my project
t
Will you have duplicated types in this case?
r
No, my project works correctly on pre.733 where there is no
JsContextFunction
. I cannot update project to pre.755 because of error, so for now I'll just copy
JsContextFunction
and test how it works without library. When we update our project to Kotlin 2.0 I'll try to update JS to newest pre.xxx version and remove JsContextFunction
t
Probably you have transitive dependency on old wrappers? It can produce such errors
r
It is possible, we will update all our libraries in a few days, when we update to Kotlin 2.0, so I'll know for sure
Is there any example of how to use JsContextFunction? Should I just create is with jsoJsContextFunction? What does those generic parameter means? "in C, in A : JsTuple, out R". Edit: I did check JsContextFunctionTest.kt but I still don't understand those generic parameters
t
You can find examples in tests for issue :)
r
Yes, it took me a while but I got it now, thanks