Prototypes

I have figured out how to add function as attribute for a function

But i can’t find anything about prototypes. All i find is kotlin with javascript. That’s not what im looking for.

Example:

//What i struggle with
fun imaprototype(function: () -> (Unit)){
function()
}

should be called like this

//myTextView is a TextView Element
myTextView.imaprototype({
println(this.getWidth())
})

Is that possible in Kotlin

Higher-order functions are possible with Kotlin (aka functions that take functions as parameters).

Here’s a runnable example–be sure to try running and editing it right here on the forum:

fun runTwice(block: () -> Unit) { // I chose "block" instead of "function" or "lambda" since I like to think of code blocks. It's not better or worse than using some other name.
    block() // We can call the lambda like a function
    block.invoke() // Or we can directly call the lambda's invoke method
}

fun main() {
    runTwice ({
        println("Hello there")
    })
    runTwice { // Notice we can drop the parentheses so that the lambda looks cleaner--kind of like a normal "if" or "while" statement. 
        println("This is the time: ${System.currentTimeMillis()}")
    }
}

Hi, and thank you arocnies.
I already figured that out (i’ve edited my post already)

Please read again.

I guess i need to specify a bit.

I look fpr a way like this

var myTextViewWidth = myTextView.getWidth()
 

but instead of getWidth i want to do my own function and chain (or attach) it to the TextView element

So that i can work with this

Like

myTextView.moveMeABit({
this.x+=100f
})

//or better 
fun moveMeABit(x:Int){
   this.x=x
}
//and than
myTextView.moveMeABit(100)

only example.

And, in a perfect world i need them chainable

[code]
myTextView.moveMeAbit(100).moveMeToTop(50)

[code]

And absolute sugar would be, if there is a way with unknown types.
so

[code]
myButton.moveMeAbit(100).moveMeToTop(50)

[code]

should work as well

This suggests using prototypes isn’t possible:
https://kotlinlang.org/docs/reference/js-interop.html#declaring-static-members-of-a-class

Though not quite the same thing as prototypes in JavaScript, since it doesn’t actually add a function to TextView’s prototype, but you create use extension functions:

fun TextView.myExtensionFun(function: TextView.() -> (Unit)) {
    function()
}


myTextView.myExtensionFunction({
    println(this.getWidth())
})

// Or invoked the more kotliny way:
myTextView.myExtensionFunction {
    println(this.getWidth())
}

2 Likes

Thanks @benwoodworth Pretty close.
Maybe i can make that accesable for more than a specific object in a function like

fun addPrototype(name:String, function: () -> (Unit)) :()->Unit{
     fun name{
              function()
    }
}
addPrototype("test",{println(this.id})
myTexView.test()
myTexButton.test()

??

I mean, it should be possible. myTextView.getHeight() is also written down somewhere. So maybe there is even a kotlin import or similar to do exactly that

How about this, using generics:

fun <T : Element> T.myExtensionFun(function: T.() -> (Unit)) {
    function()
}

This will work on any type that’s a DOM Element. You could change Element to Any to work on any type, for example. Or remove : Element entirely to work on any nullable type.

1 Like

Wow, i guess that my work.
Is there a way to return this for chaining those?

Example:
myButton.CLICK({dosome}).OVER({dosomeelse})

instead of
myButton.CLICK()
myButton.OVER() and so on

I know, i pretty much try to do javscript with kotlin, but those are important oppurnities in my opinion

1 Like

Yep! You just make it return itself:

fun <T : Element> T.myExtensionFun(function: T.() -> (Unit)): T {
    function()
    return this
}
2 Likes

@benwoodworth
Seems like my hero hit the building

Thanks a thousand times

1 Like

You might be interested in reading about the scope functions if you haven’t already.

@benwoodworth’s example is a great example for implementing your own generic extension functions. If for in the future you want a simple scoping function to chain calls, you can use the stdlib functions of apply, let, with, run, and also instead of creating your own. For example, the function benwoodworth posted is equivalent to apply.

Understanding what the stdlib scoping functions do and when they’re useful is helpful in creating your own APIs.

1 Like

Yes @arocnies i went through that article. But that does.'t helped me. Benwoosworth solution does.

Because, as you’ve mentioned, i am creating an own API (or better library).

The reason is pretty simple.

I also did a library for JavaScript that helpes me a lot in my business as a javascript developer

Im absolutely new to Kotlin (or Java), but it blowed my mind and i love it

So, beside learning Kotlin from the scratch i also want to make me a “known” path. In other words:
I try to copy my javascript library for kotlin. So i maybe can do a lot of things in kotlin without changing my style.