I have a simple Kotlin class, that does not declare a companion object.
class Foo
I would love to create an extension for companion object, even if one is not specified. I do not want to create an empty companion object.
fun Foo.Companion.bar()
How?
EDIT
What is the difference between:
- Me adding an empty companion in
Foo - compiler adds an empty companion in
Foo
?
Why?
You can't. An extension, by definition, is something that extends ... something. You can't extend a non-existent entity. Remember that extension functions are basically static functions that take an instance of the extending object as the first parameter. So your example:
Would translate to:
How is that supposed to work if
Foo.Companiondoesn't exist?