-
-
Notifications
You must be signed in to change notification settings - Fork 136
Support Kotlin higher-order functions/lambdas #977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
If someone can give me a few hints about where to look, perhaps I can make the appropriate changes myself :) |
@nerfpops I don't find this feature to have a straightforward implementation. Here's why: You'd have to generate additional metadata for the generated anonymous functions. Next step is figuring out how to wire the lambda callback to the equivalent of a fat arrow function declaration, and fat arrow function declaration to a kotlin lambda. Are there cases where javascript fat arrow functions should not be treated as kotlin lambdas? Should named functions be allowed as parameters? Will there be a problem if the functor ( |
@Pip3r4o Not a Kotlin-expert in any way but I'll try to answer your questions Are there cases where javascript fat arrow functions should not be treated as kotlin lambdas? class HTML {
fun body() { ... }
}
fun html(init: HTML.() -> Unit): HTML {
val html = HTML() // create the receiver object
html.init() // pass the receiver object to the lambda
return html
}
// Example usage
html { // lambda with receiver begins here
body() // calling a method on the receiver object
} Using html(function() {
// `this` is now the class returned from html
this.body()
})
// or
html(context => {
context.body()
}) I'm sure this wouldn't be easy to implement. Could it be something that's unsupported in the start? Are there cases where javascript fat arrow functions should not be treated as kotlin lambdas? Should named functions be allowed as parameters? Will there be a problem if the functor (myFunc) expects a callback with a number of parameters, but those are not handled in the javascript function? fun myfun(callback: (arg: String) -> Void) called from javascript via // Don't care about the string in the callback
myfun(() => console.log("invoked")) would have to be turned into something like new kotlin.jvm.functions.Function1({ invoke: () => { code } }) where Function1 is important, even though the javascript callback doesn't care about the returned string. Is it possible that the functor has many overloads of different callback signatures? |
@jsommr any ideas how add TS declaration for |
NativeScript supports marshalling Java bytecode generated by Kotlin. But Kotlin has a way to specify that a function takes another function in a much better way than Java.
Like this:
It compiles to something like
where Function1 is a class containing an invoke method. Depending on how many args the function takes, it'll be called Function2, Function3 and so on.
Interacting with myfun from JavaScript would be done this way:
It would be so cool if NativeScript supported Kotlin-functions out of the box, so the callback wouldn't need to be wrapped in FunctionX and myfun could be run via
myfun(() => console.log("invoked"))
. This would make it easier to align with an Objective-C library, where blocks are supported.To support Kotlin, my app.gradle currently looks like this:
The text was updated successfully, but these errors were encountered: