I'm trying to understand the reason why an object (singleton) can't have an inner companion. For example the following code isn't compiled:
object JustAClass
{
companion object justACompanion
{
fun justAFunc() {
}
}
}
because of:
Modifier 'companion' is not applicable inside object
But if I'm omitting the companion the code is being compiled.
A companion object is just a nested object that can be referenced by its container class's name as a shortcut. This doesn't make sense if the container class is an object, because the outer object's name is how you reference it. There would be no way to distinguish between them, so marking the nested object as a companion
would provide no functionality besides the implicit name Companion
. You can still nest an object in another object to organize your code.