Am I using sealed classes correctly in this scenar...
# getting-started
c
Am I using sealed classes correctly in this scenario?
Copy code
sealed interface UserType {
  object GUEST : UserType
  class AUTHENTICATED(
    val authToken: String,
  ) : UserType
}
d
Using all-caps for the sealed subclass names is a little unorthodox. They are normally camel case like regular class names. https://kotlinlang.org/docs/sealed-classes.html Also, is it a
UserType
or just a
User
? So will you be writing
if (user is Guest) {
or
if (user.userType is Guest) {
? Could end up with some redundancy in the name if not careful.
e
if it's for consistency with enums, well those don't need to be all-caps either. the style guide is a bit ambiguous on the topic, https://kotlinlang.org/docs/coding-conventions.html#property-names
For enum constants, it's OK to use either uppercase underscore-separated names (screaming snake case) (
enum class Color { RED, GREEN }
) or upper camel case names, depending on the usage.
but the IntelliJ plugin is on the upper camel case side, https://github.com/JetBrains/intellij-community/blob/master/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/NamingConventionInspections.kt#L208
c
I will camel case them, and good point. I think
Copy code
sealed interface User {
  object Guest : User
  class Authenticated(
    val authToken: String,
  ) : User
}
makes sense. Thanks all