https://kotlinlang.org logo
Title
m

Michael Vandendriessche

03/15/2023, 10:49 AM
Hey everyone. I just wanted to know if this is a good practice.
@JsonClass(generateAdapter = true)
data class Command(
    val cmd: String
){
    companion object{
        val firstCommand = Command("command1")
        val secondCommand = Command("command2")
        val thirdCommand = Command("command3")
    }
}
I have a data class which is parsed to/from json by Moshi. some Commands contain a string which needs to be calculated but in some cases it can be a static string. I could make an enum with the default command strings or a sealed class with objects with a Command value. But this one seems more practical to use
Command.firstCommand
and it saves me an extra file/class with default values. I don't see a problem with it other than it looks a bit strange.