NativeScript / NativeScript/android

Support Kotlin higher-order functions/lambdas

Offen
#977 4 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature severity: low
Vorherrschende Sprache
C++
Sterne
563
Forks
144
Ø Merge
10 Std. 46 Min.
Gemergte PRs (30 T.)
14

Beschreibung

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:

fun myfun(callback: (arg: String) -> Void)

It compiles to something like

public final void myfun(@org.jetbrains.annotations.NotNull final kotlin.jvm.functions.Function1<? super String, Void> callback)

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:

myfun(new kotlin.jvm.functions.Function1({ invoke: () => console.log("invoked") }))

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:

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.30"
    }
}

dependencies {
  compile fileTree(dir: "libs", include: ["*.jar"])
  compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:1.2.30"
}

android {  
  defaultConfig {  
    generatedDensities = []
    applicationId = "org.nativescript.test"  
  }  
  aaptOptions {  
    additionalParameters "--no-version-vectors"  
  }  
} 

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Es werden keine Repository-Dateien oder Tests genannt. Beginne damit, das Marshalling des Java-Bytecodes in NativeScript Android und die Darstellung von kotlin.jvm.functions.Function1, Function2 und höheren Aritäten nachzuverfolgen; vergleiche dies mit dem bestehenden Pfad zum Wrapping von JavaScript-Callbacks. Erledigt ist die Aufgabe, wenn Kotlin-Funktionsparameter direkte JavaScript-Callbacks ohne manuelles Erstellen von FunctionX-Instanzen empfangen können.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, kotlin
Bereich
mobile
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.