# What's the type of an object expression?

**URL:** <https://discuss.kotlinlang.org/t/whats-the-type-of-an-object-expression/1406>\
**Category:** Uncategorized\
**Created:** [January 13, 2016, 7:17am UTC](https://discuss.kotlinlang.org/t/whats-the-type-of-an-object-expression/1406 "2016-01-13T07:17:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pniederw1](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@pniederw1](https://discuss.kotlinlang.org/u/pniederw1)\
**Post date:** [January 13, 2016, 7:17am UTC](https://discuss.kotlinlang.org/t/whats-the-type-of-an-object-expression/1406/1 "2016-01-13T07:17:52Z")

</div>

This works:

```auto
class X(n: Int) {
  val foo = object {
    val bar = n * 2
  }.bar
}

```

This does _not_ work:

```auto
class X(n: Int) {
  val foo = object {
    val bar = n * 2
  }
  fun y() = foo.bar // bar can't be resolved
}

```

If this is expected, is there another way to declare a `foo` namespace inside X (with access to X’s instance state)? Would be very handy in some cases, e.g. to group related properties in a class.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [January 13, 2016, 9:37am UTC](https://discuss.kotlinlang.org/t/whats-the-type-of-an-object-expression/1406/2 "2016-01-13T09:37:33Z")

</div>

Yes, this is expected. Properties declared in a class need to have a non-anonymous type, so if you’re not explicitly specifying the supertype of `object`, the type of `foo` in the second example will be `Any`.

For grouping related members of the class, I think it’s best to use features that don’t impact the runtime behavior of a class, such as folding regions in the IDE.

---

<div class="post-metadata">

**Author:** ![pniederw1](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@pniederw1](https://discuss.kotlinlang.org/u/pniederw1)\
**Post date:** [January 13, 2016, 10:23am UTC](https://discuss.kotlinlang.org/t/whats-the-type-of-an-object-expression/1406/3 "2016-01-13T10:23:02Z")

</div>

> [@yole](#):
>
> For grouping related members of the class, I think it’s best to use features that don’t impact the runtime behavior of a class, such as folding regions in the IDE.

What I’m looking for is a grouping that’s visible at call sites, e.g. `metrics.foo`, `metrics.bar`, `metrics.baz` with two-stage auto-completion.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [January 13, 2016, 10:35am UTC](https://discuss.kotlinlang.org/t/whats-the-type-of-an-object-expression/1406/4 "2016-01-13T10:35:05Z")

</div>

Can you use named objects?

---

<div class="post-metadata">

**Author:** ![pniederw1](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@pniederw1](https://discuss.kotlinlang.org/u/pniederw1)\
**Post date:** [January 13, 2016, 7:50pm UTC](https://discuss.kotlinlang.org/t/whats-the-type-of-an-object-expression/1406/5 "2016-01-13T19:50:15Z")

</div>

That was my initial hope, but as far as I can tell, named objects cannot access instance state (or at least constructor arguments) of the enclosing class, as in my example.

---

<div class="post-metadata">

**Author:** ![CodeAnxiety](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/codeanxiety/32/1674_2.png) [@CodeAnxiety](https://discuss.kotlinlang.org/u/CodeAnxiety)\
**Post date:** [January 14, 2016, 6:20am UTC](https://discuss.kotlinlang.org/t/whats-the-type-of-an-object-expression/1406/6 "2016-01-14T06:20:16Z")

</div>

Have you tried using an [inner class](https://kotlinlang.org/docs/reference/nested-classes.html)?

---

<div class="post-metadata">

**Author:** ![pniederw1](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@pniederw1](https://discuss.kotlinlang.org/u/pniederw1)\
**Post date:** [January 14, 2016, 6:53am UTC](https://discuss.kotlinlang.org/t/whats-the-type-of-an-object-expression/1406/7 "2016-01-14T06:53:00Z")

</div>

That would work, but declaring a private named inner class, creating a single instance, and assigning it to a private property feels a bit too indirect/verbose for my needs. I just wanted to make sure that I’m not missing a way to achieve this with object expressions or a similar language feature, given that `object { val foo = 1 }.foo` actually works.

I think it’s safe to say by now that there is no easier way than an inner class. Thanks for the suggestion!
