# Sealed inner classes

**URL:** <https://discuss.kotlinlang.org/t/sealed-inner-classes/2371>\
**Category:** Language Design\
**Created:** [March 8, 2017, 3:52pm UTC](https://discuss.kotlinlang.org/t/sealed-inner-classes/2371 "2017-03-08T15:52:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nanodeath](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nanodeath/32/1803_2.png) [@nanodeath](https://discuss.kotlinlang.org/u/nanodeath)\
**Post date:** [March 8, 2017, 3:52pm UTC](https://discuss.kotlinlang.org/t/sealed-inner-classes/2371/1 "2017-03-08T15:52:42Z")

</div>

What’s the official plan with sealed inner classes? According to [this issue](https://youtrack.jetbrains.com/issue/KT-16233), [this issue](https://youtrack.jetbrains.com/issue/KT-15788), a couple other random issues, and [this thread](https://discuss.kotlinlang.org/t/cant-compile-generic-nested-inner-sealed-class/2267) it seems like they might be on their way out. Is that correct? They seem pretty handy to me, especially in the generics case referenced in the linked thread (of which I have a similar example in my own code).

---

<div class="post-metadata">

**Author:** ![Stanislav.Erokhin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/stanislav.erokhin/32/1764_2.png) [@Stanislav.Erokhin](https://discuss.kotlinlang.org/u/Stanislav.Erokhin)\
**Post date:** [March 9, 2017, 9:33am UTC](https://discuss.kotlinlang.org/t/sealed-inner-classes/2371/2 "2017-03-09T09:33:42Z")

</div>

In kotlin 1.0, where subclasses of sealed classes can be declared only inside such class inner sealed classes has no sense (according [KT-16233](https://youtrack.jetbrains.com/issue/KT-16233)). In 1.1 we allow subclasses for sealed classes outside sealed classes, but we do it only for top-level sealed classes for now.  
If we allow it for not top-level sealed classes, then we will allow inner sealed classes too.  
But it is open design issue, and I’m almoust sure, that in 1.2 there will be no changes in this area.

Note that you can declare inner class with private constructor and it should partially solve your use-case.

---

<div class="post-metadata">

**Author:** ![nanodeath](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nanodeath/32/1803_2.png) [@nanodeath](https://discuss.kotlinlang.org/u/nanodeath)\
**Post date:** [March 9, 2017, 5:39pm UTC](https://discuss.kotlinlang.org/t/sealed-inner-classes/2371/3 "2017-03-09T17:39:52Z")

</div>

Alright, thanks for your response. I’m not really sure what I can offer, other than use cases I suppose.

One pattern that seems like it would be super useful is to use sealed inner classes with coroutine actors. Here’s a stripped-down version of a class I’ve been toying with:

```kotlin
class BroadcastChannel<T>(context: CoroutineContext = CommonPool) {
    private sealed inner class Msg {
        inner class Subscribe(val channel: SendChannel<T>): Msg()
        inner class Broadcast(val obj: T): Msg()
    }

    private val mainActor = actor<Msg>(context) {
        val subscribers = linkedSetOf<SendChannel<T>>()
        for (msg in channel) {
            when (msg) {
                is Msg.Subscribe -> subscribers.add(msg.channel)
                is Msg.Broadcast -> subscribers.forEach { it.send(msg.obj) }
            }
        }
    }

    suspend fun subscribe(channel: SendChannel<T>) {
        mainActor.send(Msg.Subscribe(channel))
    }

    suspend fun send(obj: T) {
        obj!! // Seems to think obj is nullable otherwise
        mainActor.send(Msg.Broadcast(obj))
    }
}

```

This fails primarily because `Msg.Subscribe()` doesn’t work – it needs an instance of `Msg` to instantiate `Subscribe`. Adding an `object` inside `Msg` doesn’t work either because it doesn’t really make sense, plus [KT-16232](https://youtrack.jetbrains.com/issue/KT-16232). Even if `Msg` wasn’t sealed we’d still have this problem, though.

Changing

```kotlin
    private sealed inner class Msg {
        inner class Subscribe(val channel: SendChannel<T>): Msg()
        inner class Broadcast(val obj: T): Msg()
    }

```

to

```kotlin
    private sealed inner class Msg
    inner class Subscribe(val channel: SendChannel<T>): Msg()
    inner class Broadcast(val obj: T): Msg()

```

doesn’t work for the reason you mentioned already – the compiler simply doesn’t allow it.  
However, this _does_ work: (and is what I think you were suggesting)

```kotlin
    private open inner class Msg
    private inner class Subscribe(val channel: SendChannel<T>): Msg()
    private inner class Broadcast(val obj: T): Msg()

```

The main unpleasantness being that `Msg` is now `open` when it doesn’t really need to be, and for organizational reasons I’d _like_ `Subscribe` and `Broadcast` to be grouped together somehow, but this isn’t a requirement.

---

<div class="post-metadata">

**Author:** ![elizarov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/elizarov/32/2164_2.png) [@elizarov](https://discuss.kotlinlang.org/u/elizarov)\
**Post date:** [March 9, 2017, 9:59pm UTC](https://discuss.kotlinlang.org/t/sealed-inner-classes/2371/4 "2017-03-09T21:59:10Z")

</div>

I don’t understand why Msg has to be inner in this code. Please, explain. It seems that it will work just as expected if you just drop inner modifier.

---

<div class="post-metadata">

**Author:** ![nanodeath](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nanodeath/32/1803_2.png) [@nanodeath](https://discuss.kotlinlang.org/u/nanodeath)\
**Post date:** [March 9, 2017, 10:16pm UTC](https://discuss.kotlinlang.org/t/sealed-inner-classes/2371/5 "2017-03-09T22:16:34Z")

</div>

The `T` generic type doesn’t work unless it’s inner.

---

<div class="post-metadata">

**Author:** ![elizarov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/elizarov/32/2164_2.png) [@elizarov](https://discuss.kotlinlang.org/u/elizarov)\
**Post date:** [March 10, 2017, 7:40am UTC](https://discuss.kotlinlang.org/t/sealed-inner-classes/2371/6 "2017-03-10T07:40:10Z")

</div>

Ah. Makes sense. Thank you. The solution I would suggest is to remove `inner` modifier, but make `Msg` generic in `T` instead. The following code seems to compile as needed:

```auto
class BroadcastChannel<T>(context: CoroutineContext = CommonPool) {
    private sealed class Msg<T> {
        class Subscribe<T>(val channel: SendChannel<T>): Msg<T>()
        class Broadcast<T>(val obj: T): Msg<T>()
    }

    private val mainActor = actor<Msg<T>>(context) {
        val subscribers = linkedSetOf<SendChannel<T>>()
        for (msg in channel) {
            when (msg) {
                is Msg.Subscribe -> subscribers.add(msg.channel)
                is Msg.Broadcast -> subscribers.forEach { it.send(msg.obj) }
            }
        }
    }

    suspend fun subscribe(channel: SendChannel<T>) = mainActor.send(Msg.Subscribe(channel))
    suspend fun send(obj: T) = mainActor.send(Msg.Broadcast(obj))
}

```

The only non-trivial problem with this code, is that IDEA correctly reports in `Msg<T>` that `T` is not used, however you cannot simply remove it, or you will not be able to nicely use `when` on it (you’ll have to do unchecked casts).

---

<div class="post-metadata">

**Author:** ![nanodeath](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nanodeath/32/1803_2.png) [@nanodeath](https://discuss.kotlinlang.org/u/nanodeath)\
**Post date:** [March 10, 2017, 4:46pm UTC](https://discuss.kotlinlang.org/t/sealed-inner-classes/2371/7 "2017-03-10T16:46:43Z")

</div>

Okay, this technically works (and is probably what I’ll end up doing), but now the `T` in `Msg<T>` isn’t actually the same `T` as `BroadcastChannel<T>`'s `T` (meaning I think `Msg<T>` is defining a _new_ `T` type). However, that actually doesn’t seem to matter, but it’s mildly confusing – the following works just fine:

```kotlin
class BroadcastChannel<T>(context: CoroutineContext = CommonPool) {
    private sealed class Msg<Ignored> { // can be generic on...anything
        class Subscribe<T>(val channel: SendChannel<T>): Msg<T>()
        class Broadcast<T>(val obj: T): Msg<T>()
    }
  // snip
}

```

So `Subscribe` can continue to have a generic type that matches the _outer_ `T`. Cool, good enough for now. Thanks!

---

<div class="post-metadata">

**Author:** ![elizarov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/elizarov/32/2164_2.png) [@elizarov](https://discuss.kotlinlang.org/u/elizarov)\
**Post date:** [March 10, 2017, 9:53pm UTC](https://discuss.kotlinlang.org/t/sealed-inner-classes/2371/8 "2017-03-10T21:53:15Z")

</div>

The _declaration_ of `Msg<T>` indeed introduces a new generic type type `T` that is unrelated to `T` in `BroadcastChannel<T>` at first. However, the following declaration is what unites them:

```auto
mainActor = actor<Msg<T>>

```

It declares that an actor is going to listen to messages of type `Msg<T>`, where this `T` is coming from `T` in `BroadcastChannel<T>`.

---

<div class="post-metadata">

**Author:** ![cubuspl42](https://avatars.discourse-cdn.com/v4/letter/c/f19dbf/32.png) [@cubuspl42](https://discuss.kotlinlang.org/u/cubuspl42)\
**Post date:** [November 17, 2021, 11:38am UTC](https://discuss.kotlinlang.org/t/sealed-inner-classes/2371/9 "2021-11-17T11:38:15Z")

</div>

@elizarov Your suggestion to add `<T>` to `Msg` class declaration works, but please note that this is a very simple case. When the `BroadcastChannel` class generic parameters are more complex, for example…

```auto
class BroadcastChannel<
   SomeType1 : Foo<SomeType1>,
   SomeType2 : Foo<SomeType2>,
   ...,
>

```

…and you have more than one nested class that needs access these type parameters, then “inlining” them into every single nested class doesn’t scale well.
