hi, Is it possible to print the names of my custom...
# getting-started
hi, Is it possible to print the names of my custom colors within a list?
Copy code
val red_blood = 0xFFb92b27
 val red_orange = 0xFFb12b11
 val colors =  listOf(Color(red_blood),Color(red_orange) ...)
print: red_blood, red_orange, ...
Variable names are arbitrary, and not really accessible this way (unless you use reflection but that wouldn't be advised). What are you trying to achieve here? Depending on what you want, there could be several solutions. You could define a class which actually stores a name as well as the color value, or map those color instances to their name in a map (which could be done automatically using property delegates).
If your goal is only to access the list of names, I would go for declaring those colors inside an
and use property delegates to automatically add the color names into a list that you can later print
It is for an app that serves as an example of colors (not production code), to print the names of the colors used. To not create more classes ... reflection is the way? (sorry i'm new in kotlin)
Reflection is definitely way more burden than creating an extra class, believe me
Here is what I had in mind:
Copy code
class DumbColorGetter(val value: Long) : ReadOnlyProperty<MyColors, Long> {
    override operator fun getValue(thisRef: MyColors, property: KProperty<*>): Long = value

object MyColors {
    private val _allNames = mutableListOf<String>()
    val all get() = _allNames
    class NamedColor(val value: Long) {
        operator fun provideDelegate(thisRef: MyColors, prop: KProperty<*>): ReadOnlyProperty<MyColors, Long> {
            return DumbColorGetter(value)
    val red_blood by NamedColor(0xFFb92b27)
 	val red_orange by NamedColor(0xFFb12b11)
This way you can have the list of all your color names using
I used
because those were the types of your color constants, but you could use
That being said, if you want to do anything else with these names, you will most likely need a way to get a color by its name etc. In this case you could store these colors in a map instead of a list.
I appreciate the code and the explanation, thanks!!