Is there any way to use this `sealed class` to us...
# announcements
j
Is there any way to use this
sealed class
to use it as a parent?
Copy code
sealed class ClientName {
    data class normalClass(val name: String) : ClientName()
    object SpecificObject : ClientName()
    object AnotherObject : ClientName()
}
So now, I need to have the same objects and class but different output, so on my extension function : That is something like this :
Copy code
fun ClientName.toText(): String = when (this) {
    is ClientName.SpecificObject -> "Print1"
    is ClientName.AnotherObject -> "Print2"
    is ClientName.normalClass -> if (this.name == "") "hello" else "Bye"
}
So now I see that I'd need different types, it means that
SpecificObject
,
AnotherObject
and
normalClass
are goinna print different things depends of the type, so what I thought is create another
sealed class
like :
Copy code
sealed class Type1 : ClientName()
sealed class Type2 : ClientName()
And then in that extension
toText
check if is
Type1
then do X and if it's
Type2
do Y. But I do not know how to do it to get the same values and same clases as the
ClientName
what I'm missing?
s
With
Copy code
sealed class ClientName {
    data class normalClass(val name: String) : ClientName()
    object SpecificObject : ClientName()
    object AnotherObject : ClientName()
}

sealed class Type1 : ClientName()
sealed class Type2 : ClientName()
Then
Copy code
fun ClientName.toText(): String = when (this) {
    is ClientName.SpecificObject -> "Print1"
    is ClientName.AnotherObject -> "Print2"
    is ClientName.normalClass -> if (this.name == "") "hello" else "Bye"
    is Type1 -> { X }
    is Type2 -> { Y } 
}
j
Yes, that's exactly what I've tried but I can not use
Type1.SpecificObject
It says
Unresolved reference SpecificObject
s
? That is not possible, because SpecificObject is ‘inside’ ClientName…. And there is only one instance of it and that is
ClientName.SpecificObject
. I think you may need to rethink your sealed class hierarchy
You can’t ‘override’
SpecificObject
in
Type1
… it is a (static/global) singleton.
j
What I can do is inside the new sealed classes add the same stuff that is inside of
ClientName
... but I do not know if it's good to do that
I mean, what I was expecting is to get these objects/class from
ClientName
t
Note that if you need a specific behavior for each subclass of a sealed class, you could also define an
abstract fun
in the sealed class (i.e., use polymorphism):
Copy code
sealed class ClientName {
    abstract fun toText(): String

    data class NormalClass(val name: String) : ClientName() {
        override fun toText(): String = if (name.isEmpty()) "hello" else "Bye"
    }

    object SpecificObject : ClientName() {
        override fun toText(): String = "Print1"
    }
}
d
This one goes into nesting:

https://www.youtube.com/watch?v=uGMm3StjqLI

j
So I have to put all inside of a sealed class, right?
s
There is no need to put it 'inside' a sealed class. Putting it 'inside' or 'outside/top-level' only affects the namespacing (ie needing it to prefix it with "ClientName.").
j
But I was afraid because I was feeling that I'm repeating myself... Like I have this
Copy code
sealed class ClientName {
    data class normalClass(val name: String) : ClientName()
    object SpecificObject : ClientName()
    object AnotherObject : ClientName()
}
sealed class Type1 : ClientName()
sealed class Type2 : ClientName()
But then I have to put the
data class normalClass..
inside of each type
Right @streetsofboston?
s
You could, but then you'll have 3 (one each for ClientName, Type1, Type2) separate classes. (Their namespaces are different "ClientName.normalClass", "Type1.normalClass", etc)