val test: Int.(String) -> Int = {
plus((this))
}
When defining this type of extension function
, how can I use arguments( Here, the argument of type String)
inside the block?
When defining extension functions
at the same time as declaration like this, can only this
be used?
You can access it using it
:
val test: Int.(String) -> Int = {
println("this = $this")
println("it = $it")
42
}
fun main() {
println("result = " + 1.test("a"))
}
This will output
this = 1
it = a
result = 42
An alternative is to introduce a lambda-parameter:
val test: Int.(String) -> Int = { s ->
println("this = $this")
println("s = $s")
42
}
fun main() {
println("result = " + 1.test("a"))
}
This will output
this = 1
s = a
result = 42