# Is there a reason for not allowing inner+data classes?

**URL:** <https://discuss.kotlinlang.org/t/is-there-a-reason-for-not-allowing-inner-data-classes/2526>\
**Category:** Language Design\
**Created:** [April 26, 2017, 3:03am UTC](https://discuss.kotlinlang.org/t/is-there-a-reason-for-not-allowing-inner-data-classes/2526 "2017-04-26T03:03:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ricardojpereira](https://avatars.discourse-cdn.com/v4/letter/r/58f4c7/32.png) [@ricardojpereira](https://discuss.kotlinlang.org/u/ricardojpereira)\
**Post date:** [April 26, 2017, 3:03am UTC](https://discuss.kotlinlang.org/t/is-there-a-reason-for-not-allowing-inner-data-classes/2526/1 "2017-04-26T03:03:14Z")

</div>

Can’t see why this is not a “valid” combination.  
Thanks

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [April 26, 2017, 8:01pm UTC](https://discuss.kotlinlang.org/t/is-there-a-reason-for-not-allowing-inner-data-classes/2526/2 "2017-04-26T20:01:10Z")

</div>

Inner classes have a hidden reference to an instance of the outer class, from which they were instantiated.  
When an inner class is also a data class, this leads to a question, whether that reference is one of the data class component properties?  
And whether that reference affects how the functions `equals`, `hashCode`, `toString`, `componentN` of the data class are generated depends on the answer on this question.

---

<div class="post-metadata">

**Author:** ![reitzig](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/reitzig/32/6970_2.png) [@reitzig](https://discuss.kotlinlang.org/u/reitzig)\
**Post date:** [April 27, 2017, 6:55pm UTC](https://discuss.kotlinlang.org/t/is-there-a-reason-for-not-allowing-inner-data-classes/2526/3 "2017-04-27T18:55:21Z")

</div>

I’m curious: what kind of useful design does this restriction prevent?

My gut feeling is that data classes should stand on their own. If they depend on state of an outer object, they are not really a _data_ classes, right?

---

<div class="post-metadata">

**Author:** ![ricardojpereira](https://avatars.discourse-cdn.com/v4/letter/r/58f4c7/32.png) [@ricardojpereira](https://discuss.kotlinlang.org/u/ricardojpereira)\
**Post date:** [April 27, 2017, 7:17pm UTC](https://discuss.kotlinlang.org/t/is-there-a-reason-for-not-allowing-inner-data-classes/2526/4 "2017-04-27T19:17:24Z")

</div>

Actually this comes from another restriction I’ve asked before in forum: [Private classes in "parallel" files in same package - #2 by pdvrieze](https://discuss.kotlinlang.org/t/private-classes-in-parallel-files-in-same-package/2517/2)

i.e. : I didn’t need the class to be inner : I actually wanted it to be module private, but be named with same name in multiple files, which as mentioned above doesn’t work. So making it inner was just a workaround to avoid the name clash…

Cheers

---

<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:** [April 27, 2017, 7:25pm UTC](https://discuss.kotlinlang.org/t/is-there-a-reason-for-not-allowing-inner-data-classes/2526/5 "2017-04-27T19:25:33Z")

</div>

You can simply make it nested inside another class. Nested classes can be data classes.

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [April 27, 2017, 7:32pm UTC](https://discuss.kotlinlang.org/t/is-there-a-reason-for-not-allowing-inner-data-classes/2526/6 "2017-04-27T19:32:10Z")

</div>

In Java terms these would be static inner classes (have access to private members of the outer class but don’t hold an instance reference)

---

<div class="post-metadata">

**Author:** ![ricardojpereira](https://avatars.discourse-cdn.com/v4/letter/r/58f4c7/32.png) [@ricardojpereira](https://discuss.kotlinlang.org/u/ricardojpereira)\
**Post date:** [April 27, 2017, 7:39pm UTC](https://discuss.kotlinlang.org/t/is-there-a-reason-for-not-allowing-inner-data-classes/2526/7 "2017-04-27T19:39:56Z")

</div>

Thanks. I understand now the difference. Is taking me some time to get familiar with all the new qualifiers… That solves problem.

Thanks

---

<div class="post-metadata">

**Author:** ![jeff](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jeff/32/5512_2.png) [@jeff](https://discuss.kotlinlang.org/u/jeff)\
**Post date:** [September 15, 2018, 8:29am UTC](https://discuss.kotlinlang.org/t/is-there-a-reason-for-not-allowing-inner-data-classes/2526/8 "2018-09-15T08:29:13Z")

</div>

I came across a use for an inner data class. In my case, I need to return multiple values from a function, both of which have a generic type. I’d like to use a destructuring declaration, and a data class is ideal for declaring the named return values and automatically providing `componentN()` methods. But only an inner class has access to the generic types defined in the outer class.

Since an inner data class wasn’t allowed, I had to either declare `componentN()` methods myself in a regular inner class. Or alternatively, define the generics again in the data class and pass the outer class definitions through in the return declaration.

For example:

```auto
class Foo<T, U> {
    fun bar(): Baz {
        ...
        return Baz(quuz, corge)
    }

    inner class Baz(val qux: T, val quux: U) {
        operator fun component1() = qux
        operator fun component2() = quux
    }
}

```

Or alternatively:

```auto
class Foo<T, U> {
    fun bar(): Baz<T, U> {
        ...
        return Baz(quuz, corge)
    }

    data class Baz<T, U>(val qux: T, val quux: U)
}

```

If inner data classes were allowed:

```auto
class Foo<T, U> {
    fun bar(): Baz {
        ...
        return Baz(quuz, corge)
    }

    inner data class Baz(val qux: T, val quux: U)
}

```

---

<div class="post-metadata">

**Author:** ![bessermt](https://avatars.discourse-cdn.com/v4/letter/b/bbe5ce/32.png) [@bessermt](https://discuss.kotlinlang.org/u/bessermt)\
**Post date:** [June 4, 2019, 8:40pm UTC](https://discuss.kotlinlang.org/t/is-there-a-reason-for-not-allowing-inner-data-classes/2526/9 "2019-06-04T20:40:08Z")

</div>

I want to create an data class that has an ID for a student. I have a data class that has an ID for the course. I can’t create a data class for student and nest the ID inside nor nest the ID for the course inside the course?

I can’t create a regular nested class because @Parcelize annotation forbids it and I have to restore state if my app is killed.
