Maybe I'm missing something, but shouldn't there b...
# language-proposals
d
Maybe I'm missing something, but shouldn't there be an
inner object
declaration? Something similar to:
Copy code
class Foo {
   val num = 1

   inner class Bar {
      val one = "something $num"
   }

   val bar = Bar()

   init {
     // use it:
     println(bar.one)
   }
}
s
The discussion was inconclusive 😄
d
Thanks for that reference... I see I'm not the only one that misses this! There wasn't any reaction from the Kotlin devs though... I wonder if this is really possible/or maybe against the concept of an
object
.
One was declined, the other is still open
d
Ok, well I'm the 7th thumbs up on that one... 🤷🏼‍♂️
Oh, on the other one there's:
The point of the current design is that a named object is always a singleton. This is not possible with inner objects, that why they are prohibited.
And using anonymous objects doesn't seem like a big problem, TBH
But I thought the thread before mentioned that anonymous objects don't work here...?
Maybe they closed that issue because the workaround doesn't work, so they might reconsider the whole proposition in the non-closed issue? Not too clear from those issues...
e
inner object Foo { ... }
is simply a confusing concept. Moreover, if you need it, you can always write:
Copy code
val Foo = object { ... }
d
I tried that too... it didn't seem to work either... in the end I needed to instantiate the inner class and use that instance... which I don't really understand why it would be that different from inner object...
Even though a regular
object
is globally scoped, how could an
inner object
be understood as not being scoped to the current instance of the containing class? That's exactly what
inner
is doing... giving access to the current instance's properties.
e
named objects are singletons.
In Kotlin
I might be wrong, though. Maybe it is not that confusing.
d
This pattern could be useful to be used as a container for some static values needing to be grouped in a class instance... say:
Copy code
class SomeViewModel : ViewModel() {
  fun someAction1() {}
  fun someAction2() {}

  inner object Buttons {
    val button1 = ActionButton(R.string.action1, ::someAction1)
    val button2 = ActionButton(R.string.action2, ::someAction2)
  }
}
which is a simplified form of my use case...
d
Thanks!