https://kotlinlang.org logo
#getting-started
Title
# getting-started
c

Colton Idle

07/23/2022, 4:02 AM
Am I using sealed classes correctly in this scenario?
Copy code
sealed interface UserType {
  object GUEST : UserType
  class AUTHENTICATED(
    val authToken: String,
  ) : UserType
}
d

David Rawson

07/23/2022, 4:40 AM
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

ephemient

07/23/2022, 5:55 AM
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

Colton Idle

07/24/2022, 12:01 AM
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
4 Views