https://kotlinlang.org logo
Title
r

Rohit Singh

03/15/2019, 11:26 AM
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

ghedeon

03/15/2019, 11:37 AM
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.
😂 2
😆 3
j

jw

03/15/2019, 12:00 PM
Sure what protein? White or wheat bread? Provolone or Swiss? What toppings? Toasted?
😂 4
d

diesieben07

03/15/2019, 12:38 PM
@Rohit Singh Your code worked fine for me. What is your problem exactly?
d

Dico

03/15/2019, 1:03 PM
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

Fredrik Larsen

03/15/2019, 1:06 PM
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

Dico

03/15/2019, 1:07 PM
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:
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

Fredrik Larsen

03/15/2019, 1:19 PM
You can flatten the hirearchy.
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

Dico

03/15/2019, 1:19 PM
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

Fredrik Larsen

03/15/2019, 1:20 PM
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

Dico

03/15/2019, 1:21 PM
I have a preference to keep the file to 1 declaration as that is cleaner in the IntelliJ project view.
f

Fredrik Larsen

03/15/2019, 1:22 PM
I see.
d

Dico

03/15/2019, 1:22 PM
Sometimes I add more declarations, collecting connected stuff in one file, but often I keep it down to one.
r

Rohit Singh

03/15/2019, 2:17 PM
@diesieben07 please read the messages above 👆
d

diesieben07

03/15/2019, 2:18 PM
I did... Why?
r

Rohit Singh

03/15/2019, 2:21 PM
It only works when parent class is in same file
d

diesieben07

03/15/2019, 2:22 PM
Yes, that's how sealed classes work...
r

Rohit Singh

03/15/2019, 2:23 PM
yeah just wanted to know if its possible to have it in different file and work
d

diesieben07

03/15/2019, 2:27 PM
Since sealed classes are specifically designed to not allow that the answer is no.
✔️ 1