Is there a way to create static extension function...
# announcements
e
Is there a way to create static extension function to existing class?
r
Only if the class had a companion object
Copy code
fun String.Companion.two(string: String) = string + string

val s = String.two("foo")
😂 1
👍 1
x
Out of curiosity, what would be the purpose of creating a static extension function?
t
Doesn't pollute the autocompletion and works with classes not under your control
m
Just remember that if the class isn't under your control, it has to have a Companion defined already, or this won't compile.
☝️ 1
stdlib classes all do AFAIK.
🚫 2
m