https://kotlinlang.org logo
#announcements
Title
# announcements
j

Jiri Bruchanov

11/28/2019, 6:10 PM
Hi, wondering if I can have anonymous/named extension function like following
Copy code
val x = fun Int.() { this.toString() }
1.x()
can be 'x' defined as anonymous/named extension property ? so I could have
Copy code
1.x
?
👀 1
m

Mike

11/28/2019, 6:16 PM
You would define it on Any if you want it available on everything. But remember that this will add to the namespace/help for everything.
Copy code
fun Any.x() { this.toString() }
j

Jiri Bruchanov

11/28/2019, 6:20 PM
well, isn't it same as my 1st example ? basically i'm looking for
Copy code
fun test() {
  val x = fun Int.() { this.toString() }
  1.x()
  val y = //?
  1.y
}
in class I'd have easily something like
Copy code
class Test {
    val Int.y get() = toString()

    fun test() {            
        val x = fun Int.() { this.toString() }
        1.x()
        1.y 
    }
}
But looking if there is away to define it in the scope of the 'test' function
s

Shawn

11/28/2019, 6:23 PM
local extension properties are explicitly disallowed
👍 1
j

Jiri Bruchanov

11/28/2019, 6:24 PM
@Shawn ah ta, kinda glad that they are 😄