https://kotlinlang.org logo
#announcements
Title
# announcements
j

Jon

08/09/2019, 10:31 PM
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

karelpeeters

08/09/2019, 10:32 PM
Same in Kotlin, convert to an enum at the earliest possible opportunity.
4
j

Jon

08/09/2019, 10:32 PM
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

Mark Murphy

08/09/2019, 10:34 PM
A
sealed
class is basically a super-charged
enum
. Both offer compile-time type safety.
👍 2
k

karelpeeters

08/09/2019, 10:34 PM
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

Jon

08/09/2019, 10:35 PM
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

karelpeeters

08/09/2019, 10:36 PM
Yes. This or #getting-started, the distinction is kind of blurry
☝️ 2
2 Views