https://kotlinlang.org logo
#getting-started
Title
# getting-started
m

Manuel Pérez Alcolea

02/21/2023, 9:55 PM
Has it ever been possible calling an extension function this way?
Copy code
fun String.myFunction(param: Int) { }

// ...

"receiver".myFunction(5)

// but instead...
myFunction("receiver", 5)
I'm positive I was able to do that from Kotlin.
e

ephemient

02/21/2023, 9:59 PM
no. but if it were a value
Copy code
myFunction: String.(param: Int) -> Unit
instead (such as a property or function parameter), then
"receiver".myFunction(5)
and
myFunction("receiver", 5)
would be equivalent
🤯 1
r

Ruckus

02/21/2023, 10:00 PM
Not on a function call directly, but you can on lambdas or function references:
Copy code
fun String.myFunction(param: Int) {}

// ...

fun main() {
    val myFunction = String::myFunction
    
    "receiver".myFunction(5)

    // but instead...
    myFunction("receiver", 5)
}
Ah, beat me to it 🙂
m

Manuel Pérez Alcolea

02/21/2023, 10:02 PM
Ah, that was probably what I was thinking about. Thanks.
v

Vampire

02/21/2023, 11:52 PM
Or you had Groovy in mind. 😄
k

Klitos Kyriacou

02/22/2023, 3:06 PM
Why is it allowed for lambdas?
4 Views