https://kotlinlang.org logo
Title
o

orestisfou

11/02/2018, 12:01 PM
hello, is it possible to wrap on a sealed class classes of external libs?
g

gildor

11/02/2018, 12:10 PM
wrap on?
o

orestisfou

11/02/2018, 1:22 PM
so let's say I use a lib that has classes A and B. Now I want to create a sealed class that contains those two classes. Is this possible and if yes what is the syntax? For example if a lib contains
Dog
and
Cat
can I create a sealed class called
Animal
that wraps those two types?
g

gildor

11/02/2018, 1:23 PM
Yes, sure:
sealed class Animal
data class DogWrapper(val dog: Dog) : Animal()
data class CatWrapper(val cat: Cat) : Animal()
o

orestisfou

11/02/2018, 1:35 PM
thanks
g

gildor

11/02/2018, 1:37 PM
also it can be something like:
sealed class Animal<T> {
    abstract val value: T
}
data class DogWrapper(override val value: Dog) : Animal<Dog>()
data class CatWrapper(override val value: Cat) : Animal<Cat>()
No big difference, but at least you can get
value
, but only as
Any
o

orestisfou

11/02/2018, 1:42 PM
cool, thank you
r

radityagumay

11/03/2018, 2:25 PM
Accepted answer