Michael de Kaste
07/06/2023, 10:09 AMJavier
07/06/2023, 10:14 AMMatteo Mirk
07/06/2023, 10:32 AMin the process of making an enum typesafehuh... what? Enum constants are already typesafe, what did you mean? 🤔
alex.krupa
07/06/2023, 10:33 AMFor enum constants, it's OK to use either uppercase underscore-separated names (screaming snake case) () or upper camel case names, depending on the usage.enum class Color { RED, GREEN }
alex.krupa
07/06/2023, 10:34 AMJavier
07/06/2023, 10:35 AMMichael de Kaste
07/06/2023, 11:35 AMLetter
the values for this letter are things like: A, B, C, D, E, and F
these domains are being used throughout the entire application
Now, I have different container classes and logic that should only work with vowels
. I can introduce a 'isVowel' boolean on the enum.
I also have different container classes and logic for things ending in the 'ee' sound; likewise, I can introduce a field for that.
However, the specific combination of isVowel and isSoundE only occurs in some situations
Lets say I have a function:
fun shouldOnlyWorkWithVowels(example: VowelClassContainingTheField): String? {
if(example.letter.isVowel){
return when(example.letter){
A -> example.aThing()
E -> example.eThing()
else -> error("this should never occur")
}
} else {
return null
}
}
If I make letter sealed with things like:
sealed interface Letter
sealed interface Vowel : Letter
sealed interface SoundE : Letter
object A: Vowel
object B: SoundE
object C: SoundE
object D: SoundE
object E: Vowel, SoundE
object F: Letter
I can, typesafe, work with subsections of this sealed class; as if they were an enum.
fun shouldOnlyWorkWithVowels(example: VowelClassContainingTheField): String {
return when(example.letter){
A -> example.aThing()
E -> example.eThing()
}
}
in fact, I can do more complicated things like:
private fun handle(letter: Letter){
when(letter){
A -> "special A case"
is Vowel -> "the rest of the vowels"
is SoundE -> "the rest of the ee sounding cases"
F -> "The final special case"
}
}
Javier
07/06/2023, 12:20 PMMatteo Mirk
07/06/2023, 12:22 PMYoussef Shoaib [MOD]
07/06/2023, 12:24 PMsealed interface Vowel
Javier
07/06/2023, 12:24 PMMatteo Mirk
07/06/2023, 12:25 PMJavier
07/06/2023, 12:26 PMMichael de Kaste
07/06/2023, 12:50 PMephemient
07/06/2023, 4:14 PMobject
is the same as for `class`/`interface`: upper camel case