# Why can't I do this in object expressions?

**URL:** <https://discuss.kotlinlang.org/t/why-cant-i-do-this-in-object-expressions/24148>\
**Category:** Language Design\
**Created:** [February 9, 2022, 1:03pm UTC](https://discuss.kotlinlang.org/t/why-cant-i-do-this-in-object-expressions/24148 "2022-02-09T13:03:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![banjjak2](https://avatars.discourse-cdn.com/v4/letter/b/a3d4f5/32.png) [@banjjak2](https://discuss.kotlinlang.org/u/banjjak2)\
**Post date:** [February 9, 2022, 1:03pm UTC](https://discuss.kotlinlang.org/t/why-cant-i-do-this-in-object-expressions/24148/1 "2022-02-09T13:03:19Z")

</div>

> **[Object expressions and declarations | Kotlin](https://kotlinlang.org/docs/object-declarations.html#using-anonymous-objects-as-return-and-value-types)**

```auto
val helloWorld = object {
    val a = 0
}

private val helloWorld2 = object {
    val a = 0
}

fun main() {
    val helloWorld3 = object {
        val a = 0
    }

    // a1
    println(helloWorld.a) // no access
    // a2
    println(helloWorld2.a) // accessible
    // a3
    println(helloWorld3.a) // accessible
}

```

Why is it not accessible from “a2”, “a3”?

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [February 9, 2022, 1:10pm UTC](https://discuss.kotlinlang.org/t/why-cant-i-do-this-in-object-expressions/24148/2 "2022-02-09T13:10:24Z")

</div>

The page you mention says:

If this function or property is public or private inline, its actual type is:

- `Any` if the anonymous object doesn’t have a declared supertype

Hence your `helloWorld` has type `Any`.

---

<div class="post-metadata">

**Author:** ![banjjak2](https://avatars.discourse-cdn.com/v4/letter/b/a3d4f5/32.png) [@banjjak2](https://discuss.kotlinlang.org/u/banjjak2)\
**Post date:** [February 9, 2022, 1:44pm UTC](https://discuss.kotlinlang.org/t/why-cant-i-do-this-in-object-expressions/24148/3 "2022-02-09T13:44:34Z")

</div>

Thank you for your reply 😀 . But I wonder why there is such a constraint.

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [February 9, 2022, 1:52pm UTC](https://discuss.kotlinlang.org/t/why-cant-i-do-this-in-object-expressions/24148/4 "2022-02-09T13:52:01Z")

</div>

No idea, however the usual syntax for defining a singleton is:

```kotlin
object helloWorld {
    val a = 0
}

```

which works the way you like. And is shorter.

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [February 9, 2022, 10:30pm UTC](https://discuss.kotlinlang.org/t/why-cant-i-do-this-in-object-expressions/24148/5 "2022-02-09T22:30:26Z")

</div>

Let’s start with why the property is not accessible.

Object expression creates anonymous type. This means not only that this type does not have a name, but it doesn’t at all exist. Well, technically it exists, but not from the user perspective. So if there is no type that can describe what is `helloworld`, then it can only be `Any`. In other words: to access `a` property of some variable, it has to be of type that has `a` property. But in this case there is no usable type with `a` property.

If object expression extends from a supertype, then the variable can be of this supertype, because supertype is not anonymous and can be referenced normally. But still, if this object expression adds new members, they won’t be visible, because the type created by the object expression is itself anonymous, so we can only reference members of the supertype.

So… why in some cases this is all not at all true?

My understanding is that this is for user convenience. In some cases Kotlin performs advanced type inference and provides some flexibility to the type system. But, this is usually limited to a more local scope, because it would be too complicated or too error-prone to use it on a wider scale. Smart casts is a good example. They provide additional information to the type system, they even support union types, but this is all local. At the point where we assign such local variable to a property, all this advanced information is lost.

It is similar with object expressions. Kotlin allows to be more flexible if the variable is private, because the risk of doing something bad is relatively low. And it is easier for the compiler to support such case. But if you try to make the variable available from the outside world, you lose this additional flexibility.

---

<div class="post-metadata">

**Author:** ![banjjak2](https://avatars.discourse-cdn.com/v4/letter/b/a3d4f5/32.png) [@banjjak2](https://discuss.kotlinlang.org/u/banjjak2)\
**Post date:** [February 10, 2022, 1:01am UTC](https://discuss.kotlinlang.org/t/why-cant-i-do-this-in-object-expressions/24148/6 "2022-02-10T01:01:21Z")

</div>

@al3c @broot Thank you for taking the time to reply!

---

<div class="post-metadata">

**Author:** ![Ayfri](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ayfri/32/10382_2.png) [@Ayfri](https://discuss.kotlinlang.org/u/Ayfri)\
**Post date:** [February 15, 2022, 5:33pm UTC](https://discuss.kotlinlang.org/t/why-cant-i-do-this-in-object-expressions/24148/7 "2022-02-15T17:33:37Z")

</div>

Where do you put this code ? It doesn’t work for me in Kotlin/JVM 1.6.10 for a top-level variable or a local variable in function 🤔

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [February 15, 2022, 5:39pm UTC](https://discuss.kotlinlang.org/t/why-cant-i-do-this-in-object-expressions/24148/8 "2022-02-15T17:39:03Z")

</div>

my bad I corrected the example 🙂 you need to use `object` and not `val`
