I have a data class with some fields of enum type ...
# announcements
v
I have a data class with some fields of enum type and some are just Strings. I need to create an object with just one String field and thus not include enum fields. How should I declare enum fields in my data class so that they will have an empty default value?
I can’t assign a certain enum value to this field to be its default. I know that I can make this field nullable. But, maybe there’s a better solution than
null
?
z
It’s pretty idiomatic to use a nullable type to indicate that a value may not be present, I would just make them nullable. Even if you have the option of adding another enum value that represents “no value”, the advantage of using a nullable type instead is that you get nice syntactic sugar for dealing with the lack of a value (eg.
data.enum ?: default
instead of
if (data.enum == NO_VALUE) default else data.enum
)
💯 2
v
Got it. Thanks for the reply!