Is there any annotations to cast `this` as another...
# javascript
l
Is there any annotations to cast
this
as another class? Now I'm writing
Copy code
fun (){
    val component = js("this") as VueComponent
    component.doSmth()
}
I want
Copy code
@JsChangeThis(VueComponent)
fun (){
    this.doSmth()
}
b
is it anonymous function / lambda ?
l
Let's say yes.
b
anyway the answer is “no, we don’t have a such annotation”
just wondering why do you need it?
l
Vue uses POJsO for configuring components.
this
is changed at runtime. Without any hacks
this
will resolves as POJsO, but I need it to be VueComponent (external declaration)
b
Could you please share links to examples with such code and/or to docs?
l
For example https://vuejs.org/v2/guide/computed.html#Basic-Example
Copy code
var vm = new Vue({
  el: '#example',
  data: {
    message: 'Hello'
  },
  computed: {
    // a computed getter
    reversedMessage: function () {
      // `this` points to the vm instance
      return this.message.split('').reverse().join('')
    }
  }
})
b
Thank you! Unfortunately, I don’t have any good solution for this now, but we’ll think about this case.
l
For example you could develop annotation for methods, that will change
this
for annotated method and overriden methods. So we could use it in interfaces.
b
We prefer to avoid to do platform-specific hacks in FE
l
What is FE?
b
Frontend of the compiler
l
Heh... You have to create usefull hacks like that to provide community ability to use existed js frameworks. You can't lock frontend within kotlin. This hack could be used in other languages, that could change
this
. (I don't know witch :) )
Did react change
this
?
b
You can’t lock frontend within kotlin.
We don’t think so and it’s the reason why the interop is important​ for us.
This hack could be used in other languages
Do you mean frameworks / libraries?
you can write some utility like:
Copy code
fun <T, R> fix(f: T.() -> R): T.() -> R {
    val fs = f.toString()
    val m = "function\\s*[^(]*\\(([^,)]*)\\s*,?\\s*([^)]*)\\)\\s*{".toRegex().find(fs)
    println(m?.groupValues)
    val firstArg = m?.groupValues?.get(1) ?: ""
    val otherArgs = m?.groupValues?.get(2) ?: ""
    val body = fs.substring(m?.groupValues?.get(0)?.length ?: 0, fs.length - 1)
    val r = js("Function")(otherArgs, "    $firstArg=this;" + body)
    return r
}
and use this way:
Copy code
val foo = fix<VueComponent, Int> {
    componentProp + 1
}