Possible Inner Object Construct

Hi,
I am actually using kotlin 1.4.10 and am missing a feature?
It is needed for structuring issues (hope so, that’s the right word). For example:

class MyTestData (val foo: Foo) // dependencies
{
  inner object case1 {
    val bar = foo.bar()
  }

  inner object case2 {
    val bar = foo.someOtherBar()
  }
}

...

// somewhere
val data = MyTestData(FakedFoo())
assertEquals(data.case1.foo, ...)

With object alone it is not possible to access foo. “inner object” does not exist. Is it possible to get this feature in some future version?
I am asking, because there exist “inner class” and an “inner object” would be nice …

You can declare a private inner class with the behaviour you want, and then have a field of that type.

class MyTestData (val foo: Foo)
    inner class Case1_ {
        val bar = foo.bar()
    }
    val case1 = Case1_()
}

The reason for inner object not to exist is that it’s confusing imho, object instances are singletons, in your case you’d have an instance of each of these inner objects for each instance of MyTestData.

1 Like

private inner class is not possible. The inner class must be public if you want to access its fields.
Why confusing? You can write that:

class MyTestData  {
  object case1 {
    val bar = 123
  }
}

It could be a shortcut for your proposed solution.