Colton Idle
08/25/2023, 3:32 PMJoffrey
08/25/2023, 3:35 PMCasey Brooks
08/25/2023, 3:35 PMCasey Brooks
08/25/2023, 3:36 PMJoffrey
08/25/2023, 3:37 PMsealed
hierarchies (with sealed classes and/or interfaces)Casey Brooks
08/25/2023, 3:38 PMPablichjenkov
08/25/2023, 3:43 PMEnums are fancy String constants.
Sealed classes are just classes, but only have specific
sub-classes🫶 , Although they technically abstract classes
Joffrey
08/25/2023, 3:43 PMEnums are fancy String constants@Casey Brooks That's a hell of a shortcut, though 😄 They actually are a fixed set of instances of a single class, with properties and methods, possibly custom equality... that's quite a bit more than just strings
Joffrey
08/25/2023, 3:47 PMCasey Brooks
08/25/2023, 3:54 PMenum class
and a sealed class
with object
values.
The more accurate comparison of terms is:
• enum class
is similar to a sealed class
in that it gives you a way to enumate a fixed set of values, especially within a when()
block
• a single value (enum case) within an enum class is similar to String constant, or an object
• An object
within a sealed class
is similar to an enum case. It can be part of a when()
block
• A regular class
within a sealed class
has no equivalent in an enum class
. This is the thing that will help you decide when you want enums vs sealed classesWout Werkman
08/25/2023, 3:58 PMCLOVIS
08/25/2023, 4:17 PMenum class
is the same as a sealed class
which only has object
inheritorsPablichjenkov
08/25/2023, 4:58 PMCLOVIS
08/25/2023, 5:01 PMenum class Foo(val id: String) {
First("first"),
Second("second"),
;
}
vs.
sealed class Foo(val id: String) {
object First : Foo("first")
object Second : Foo("second")
}
Colton Idle
08/25/2023, 5:02 PMColton Idle
08/25/2023, 5:17 PMChris Lee
08/25/2023, 6:37 PMColton Idle
08/25/2023, 6:39 PMChris Lee
08/25/2023, 6:41 PMChris Lee
08/25/2023, 6:42 PMJavier
08/25/2023, 9:26 PMChris Lee
08/25/2023, 9:27 PMenum class Foo(val id: String) {
First("first"),
Second("second"),
;
}
Chris Lee
08/25/2023, 9:31 PMJavier
08/25/2023, 9:40 PMChris Lee
08/25/2023, 9:42 PMJavier
08/25/2023, 9:44 PMvar
with enumsChris Lee
08/25/2023, 9:45 PMenum class Foo(var x : String) {
One("a"),
Two("b")
}
Chris Lee
08/25/2023, 9:46 PMJavier
08/25/2023, 9:46 PMefemoney
08/27/2023, 10:04 PMsealed interface Trim {
companion object {
val Start: Trim = TrimImpl(...)
val End: Trim = TrimImpl(...)
val Both: Trim = TrimImpl(...)
}
}
will evolve easier than enums & also easier than the sealed interface that exposes subclasses to public API.
(advice brought to you by someone who has seen pain 😅 maintaining binary compatibility of public library APIs)Colton Idle
08/27/2023, 10:10 PMefemoney
08/28/2023, 6:01 AM