creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126684

How to use a function called "val()" in Kotlin? / extend external JS classes

I encountered a problem while trying to use Cloud Functions for Firebase with Kotlin.

When accessing data from a database trigger you use the snapshot.val(); function in Javascript. In Kotlin this is not possible because val is a keyword in Kotlin. I managed a way to go around this by using

snapshot.`val()`

This way I can call functions with any name in Kotlin. Now I think that it is really annoying to type this "grave accent" (`) thingy every time, so I tried to create an extension function.

My problem with that is that I do not know how to reference the type. In Javascript it is DeltaSnapshot or DataSnapshot when accessing the database.

How do I extend external classes or how can I call it by a different name? Typealiases won't work because they are limited to classes / instead of supporting functions.

Upvotes: 3

Views: 173

Answers (3)

try this:

js("snapshot.val()").yourKotlinCode

it works for me

Upvotes: 0

Alexey Andreev
Alexey Andreev

Reputation: 2085

Did you try following?

external class MyClass {
    @JsName("val")
    fun foo()
}

Upvotes: 3

Orelsanpls
Orelsanpls

Reputation: 23515

In javascript a class is an object aswell

snapshot['val']();

More generic

object[functionName]();

object[functionName].call(object, ...args);

object[functionName].apply(object, args);

Upvotes: 1

Related Questions