```sealed class BaseViewEffect{ object ShowVie...
# announcements
r
Copy code
sealed class BaseViewEffect{
    object ShowView : BaseViewEffect()
}

sealed class ChildViewEffect : BaseViewEffect {
    
}
anyone can help me to achieve inheritance for sealed class
@jw any ideas to solve this use case?
👎 1
g
On a side note,
@jw
would you make me a sandwich? I thought I might as well give it a try, since you're on a speed dial for everything.
😆 3
😂 2
j
Sure what protein? White or wheat bread? Provolone or Swiss? What toppings? Toasted?
😂 4
d
@Rohit Singh Your code worked fine for me. What is your problem exactly?
d
The problem is that you cant have inheritance of sealed classes without putting the children inside the sealed class. I would prefer the ability to have them in the same file :/
f
That was true before Kotlin 1.1, but now it works as long as it is in the same file. https://kotlinlang.org/docs/reference/sealed-classes.html
d
That's weird, I've had the problem of not being able to do this recently.
Well, I tried it and it works. I must have imagined it.
Ah no, theres something that does not work.
Example:
Copy code
sealed class MySealedClass {
    open val length get() = 0
    
    class SpecialBlock(override val data: ByteArray) : SpecialVariants()
    
    sealed class SpecialVariants : MySealedClass() {
        abstract val data: ByteArray
        override val length get() = data.size
    }
}
SpecialBlock
can't inherit from
SpecialVariants
bug report?
f
You can flatten the hirearchy.
Copy code
sealed class MySealedClass {
    open val length get() = 0
}

sealed class SpecialVariants : MySealedClass() {
    abstract val data: ByteArray
    override val length get() = data.size
}

class SpecialBlock(override val data: ByteArray) : SpecialVariants()
d
I understand that there's a workaround, I still would consider this inconsistent.
For me the workaround was to use
abstract
instead of
sealed
, sadly.
1
f
Fair enough. I still don't see the issue with flattening though. If you use abstract you lose compile time information about the hierarchy.
d
I have a preference to keep the file to 1 declaration as that is cleaner in the IntelliJ project view.
f
I see.
d
Sometimes I add more declarations, collecting connected stuff in one file, but often I keep it down to one.
r
@diesieben07 please read the messages above 👆
d
I did... Why?
r
It only works when parent class is in same file
d
Yes, that's how sealed classes work...
r
yeah just wanted to know if its possible to have it in different file and work
d
Since sealed classes are specifically designed to not allow that the answer is no.
✔️ 1