hey! so i have two enum classes that don't directl...
# announcements
x
hey! so i have two enum classes that don't directly depend on each other right now (obviously), but my idea is to make a relationship between the two, because they are actually directly related. let's say one is spell categories, and the other one is spells. so for example, a spell category could be fire, and the spells would only be fire-based spells (following on thread)
how would one model this? currently it's like
Copy code
enum class SpellType {
FIRE, WATER
}
enum class Spell {
FIREBALL, WATERBALL
}
but of course this doesn't fulfill my initial condition because you could do something like this
Copy code
class SpellCaster{
  val spellType: SpellType
  val spells: List<Spell>
}
and have a water spell caster have fire spells
i hope its clear enough
n
Copy code
enum class SpellType {
    FIRE, WATER
}
sealed class Spell<SpellType> {
    class FIREBALL<FIRE>
    class WATERBALL<WATER>
}
i had to change the code a bit because enum cannot have type parameter
x
that's awesome
so in the case of spellcaster, it would be something like this?:
Copy code
class SpellCaster(
    val spellType: SpellType,
    val spells: List<Spell<SpellType>>
)
? or how would you constrain it?
in the spellcaster class itself
n
that depends if you magus is only allowed to cast one type of spell you could model it like this:
Copy code
class SpellCaster<T : SpellType>{
    lateinit var spellType: T
    lateinit var spells: List<Spell<T>>
}
x
i see, that's awesome, thank you
my only concern is that i'm using javax persistence
Copy code
@Enumerated(EnumType.STRING)
for some stuff, and i'm pretty sure it's gonna begin storing them as
Copy code
FIREBALL<FIRE>
instead of as
Copy code
FIREBALL
because of the change from enum class to sealed
n
that’s another requirement 🙂
that you didn’t mention before
then, i’d avoid using
enum
x
yeah its just something i just noticed, sorry 😆
n
or if you want:
Copy code
enum class SpellType {
    FIRE, WATER
}
abstract class Spell(val type: SpellType)
x
this is perfect, thank you again
👍 1