What would be the idiomatic way to do this in Kotl...
# announcements
j
What would be the idiomatic way to do this in Kotlin? I am doing a switch on a String that has well defined values. I'd like to make sure that all these values are handled in the switch statement. I do this in several locations. In other languages, I would have created a function that converts string to some type and then switch on that type. In the "default" section of the switch statement I would have thrown some assertion to make sure all cases are handled.
k
Same in Kotlin, convert to an enum at the earliest possible opportunity.
4
j
alrighty, thanks Karel. I thought maybe there would be a type-safe compile-time check I take advantage of
like possibly a sealed class maybe?
m
A
sealed
class is basically a super-charged
enum
. Both offer compile-time type safety.
👍 2
k
You could convert to instances of a sealed class too, but that's equivalent to an enum. It's not going to make the string part any easier or safer.
👍 1
j
Thanks @karelpeeters and @Mark Murphy.
👍 1
On an unrelated note, is this the best channel to ask these types of questions? I couldn't find anything more appropriate
k
Yes. This or #getting-started, the distinction is kind of blurry
☝️ 2