mbonnin
05/30/2022, 12:26 PMclass Foo {
object bar {}
companion object {
val bar = "companion bar"
}
}
// I can read the companion bar
println(Foo.bar) // companion bar
// How do I access the object bar?
println(Foo.??)Sam
05/30/2022, 12:39 PMprintln(Foo.bar) // prints "companion"
println(Foo.bar.toString()) // prints "Foo$bar@3fa77460"
How weird is that?
So my first suggestion is Foo.bar.let { it }. The IDE warns me I'm being stupid, but it works. Though I don't think I'd want to rely on it continuing to work in future versions...mbonnin
05/30/2022, 12:40 PMFoo.bar.let { it } mind blownmbonnin
05/30/2022, 12:40 PMSam
05/30/2022, 12:40 PMmbonnin
05/30/2022, 12:44 PMwasyl
05/30/2022, 12:46 PMFoo::class.nestedClasses.single { !it.isCompanion }.objectInstance as Foo.bar
🧌wasyl
05/30/2022, 12:47 PMFoo#Bar and Foo#Companion#bar. Clearly since you’re asking you have a reason to distinguish those, I’m just curiousmbonnin
05/30/2022, 12:48 PMmbonnin
05/30/2022, 12:48 PMbar and Bar can be two different enum values...wasyl
05/30/2022, 12:51 PM_bar for the lowercase variant if there’s a collision. #notHelpfulmbonnin
05/30/2022, 12:52 PMname and ordinal already which are not allowed in kotlin enums. Was just curious if there was a better fixwasyl
05/30/2022, 12:52 PMlet { } seems like an implementation detail? toString() too, like function calls prioritise types, but raw Foo.bar prioritises properties mind blowndmitriy.novozhilov
05/30/2022, 1:47 PMhfhbd
05/30/2022, 5:04 PMmbonnin
05/30/2022, 5:05 PMimport Foo.bar as companionBar
But that still resolves to the companion. In the imports or in the code the problem is the samehfhbd
05/30/2022, 5:06 PMimport Foo.bar
class Foo {
object bar {}
companion object {
val bar = "companion bar"
}
}
fun main() {
println(Foo.bar) // companion bar
println(bar) // Foo$bar@58644d46
}mbonnin
05/30/2022, 5:14 PMimport Foo.bar was resolving to companion bar ...mbonnin
05/30/2022, 5:16 PM