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

spierce7

08/08/2019, 5:25 PM
How do I use an extension function that's nested in an object from outside that object? i.e.
Copy code
object Example {
    fun String.exampleExtension() {
    }
}

fun test(str: String) {
    str.exampleExtension() // Doesn't work. How can I use the extension function?
}
s

Shawn

08/08/2019, 5:27 PM
simplest way:
Copy code
with(Example) {
  str.exampleExtension()
}
but passing
Example
to any function that takes
T.() -> *
will work, like
apply
also might be able to do a “static” import™
don’t quote me on that one though
s

spierce7

08/08/2019, 5:44 PM
works perfectly. Thanks!
👍 1
5 Views