In kotlin, why `B.INSTANCE.foo()` is not allwoed b...
# stdlib
a
In kotlin, why
B.INSTANCE.foo()
is not allwoed but
A.Companion.foo()
is allowed. Code in 🧵 Also let me know if this is not a correct channel
Copy code
class A {
    companion object {
        fun foo() = 1
    }
}

object B {
    fun foo() = 1
}
j
It definitely isn't the right channel because it has nothing to do with the stdlib
But I don't see the discrepancy, neither let's you reference the instance
z
There's no
INSTANCE
thing in Kotlin.
Companion
is just the name of the companion object, which is optional when referring to its members (you can just do
A.foo()
.
For code golf, what
B.INSTANCE.foo()
would refer to would be:
Copy code
object B {
  object INSTANCE {
    fun foo() = 1
  }
}
But this would be pretty weird to see.
j
Yeah, INSTANCE is the JVM representation which shows up from Java
a
Thanks for the explanation