```enum class Type(val value: String, val colorCod...
# android
a
Copy code
enum class Type(val value: String, val colorCode: String) {

    BUY("B", GREEN),
    SELL("S", RED),
    UNKNOWN("", "");

    private companion object {
         private const val GREEN = "#3fc963"
        private const val RED = "#e35353"
    }
}
it is showing compile error on GREEN and RED as it should be initialized. i have init in the companion obj why is the error then?
c
Strange... using
Type.GREEN
and
Type.RED
works... 🤔
l
You are trying to use a property before the class is initialized. the enum class is trying to init but the “GREEN” property will only be available after the init
You need to declare those constants outside of the enum class
or even better, just inline them
BUY("B", "#3fc963"),
a
@Luis I inilined earlier but i thought from readability perspective giving a name to colors is always good
@Luis @Christiano is this good?
Copy code
enum class Type(val value: String, val colorCode: String) {
    BUY("B", Type.GREEN),
    SELL("S", Type.RED),
    UNKNOWN("", "");
    
    private companion object {
        private const val GREEN = "#3fc963"
        private const val RED = "#e35353"
    }
}
c
If the point is to make it readable, I'd just have colors somewhere else:
Copy code
enum class Color(val hex: String) {
  RED(...)
  GREEN(...)
}
Copy code
enum class Type(...) {
  ...
}
This way you can reuse your colors in other places of your app.
l
Yeh, if you are going to reuse the colors, I would pull them out
What I meant is that
Type
is an enum, with a fixed number of possibilities and with a fixed number of properties
Someone reading that code wouldn’t mistake the string in the enum definition by something else, since its an enum with named variables
a
Anyway i am planning to inline the colors.
a
Hi @Anshulupadhyay03. From an Android perspective hard coding the colours in your Kotlin code like this is probably not a good idea as you’re making it much harder to theme your app - whether this be to support white labels or even just day and night mode. In that regard I would avoid creating an Enum like this and have a separate class or function to lookup the colour from a resources file
a
@appmattus Great to hear from you Mat 😀. This is not for the purpose of theming inside the app. it is just for the colors used for a very specific feature and only these three colors would be possible always. Hence I am keeping it inside that enum so that it should be in the same domain where it belongs to.