is it not possible to use a reference to a field i...
# announcements
b
is it not possible to use a reference to a field in a companion object without using
.Companion
?
Copy code
class Foo {
    companion object {
        val bar: Int = 42
    }
}
// Works
val member2 = Foo.Companion::bar
// Doesn't work: unresolved reference
val member = Foo::bar
k
There's pretty big difference between class member function references (type
KFunction0<R>
) and companion function references (type
KFunction1<Foo, R>
), maybe that's why it's not allowed.
m
You could have a bar function on Foo class and in companion. How would compiler know which one? For that matter, how would you? Kotlin favours explicit definitions anyway.
b
true, but java in that situation just errors on the ambiguity
but perhaps the inner workings of companion object make that difficult
m
Or they intentionally show explicit mess over implicit?
e
I guess you’ll need Companion classifies if inside of it
b
what do you mean?
e
Foo::bar corresponds Foo#bar which is instance property Foo:Companion:bar corresponds Foo.Companion#bar which is Companion’s property I guess Kotlin requires write Companion if it is in Companion?
b
ah, yeah that's what i found...was just wondering if that was really the case (
.Companion
being required) or if i was missing something
e
But I think I misreading something — Java doesn’t require it, so Kotlin doesn’t either?
b
Right, java doesn't...but Kotlin appears to.
e
But if Kotlin doesn’t, IDEA would give you a hint that unnecessary ‘Companion’ — I believe in builtin Kotlin support works, mmm