Hi guys, I have a question about sealed class. Wh...
# android
m
Hi guys, I have a question about sealed class. What is different between these: 1. first approach:
sealed *class* Expr
data *class* Const(*val* number: *Double*) : Expr()
data *class* Sum(*val* e1: Expr, *val* e2: Expr) : Expr()
2. second approach:
sealed *class* Expr {
data *class* Const(*val* number: *Double*) : Expr()
data *class* Sum(*val* e1: Expr, *val* e2: Expr) : Expr()
}
o
First one's type is
val foo = Const(1)
second one is
val foo = Expr.Const(1)
I almost always prefer the second
m
How do you feel about performance and memory usage? I'm wondering to know the second does affect on performance or memory leak and etc.?
o
Pretty sure they don't have a difference in performance
b
Second one is composing the data classes making it more coupled with the
Expr
class, which makes sense when you are sure you don't want other implementations for those data classes
Const
&
Sum
. First one is not composing them rather keeping them de-coupled.
👏 1