Reference an object in a class by using a string?

Another way you could do this is with an enum class. The advantage over a map is that you have a data structure you can reference directly in code, so you could use HerbData.Dill as well as HerbData["Dill"]. And that will enable you to take advantage of compile-time checking and lint warnings, refactoring, exhaustive pattern matching, code completion etc, because the data is defined in your code

enum class HerbData(
    val herbName: String,
    val scientificName: String? = null,
    val dullThumbnail: Int? = null
) {
    Dill("This is Dill!", "Anethum Graveolens", R.drawable.dill_thumbnail_attr),
    Peppermint("This is Peppermint!");
    
    companion object {
        operator fun get(name: String): HerbData? =
            try { valueOf(name) } catch(e: IllegalArgumentException) { null }
    }
}

fun main() {
    // no guarantee these lookups exist, need to null-check them
    HerbData["Peppermint"]?.herbName.run(::println)
    // case-sensitive so this fails
    HerbData["peppermint"]?.herbName.run(::println)

    // this name is defined in the type system though! No checking required
    HerbData.Peppermint.herbName.run(::println)
}

>> This is Peppermint!
null
This is Peppermint!

Enum classes have that valueOf(String) method that lets you look up a constant by name, but it throws an exception if nothing matches. I added it as a get operator function on the class, so you can use the typical getter access like a map (e.g. HerbData["Dill"]). As an alternative, you could do something a bit neater:

companion object {
    // storing all the enum constants for lookups
    private val values = values()
    operator fun get(name: String): HerbData? =
        values.find() { it.name.equals(name, ignoreCase = true) }
}

You could tweak the efficiency on this (I'm just storing the result of values() since that call creates a new array each time) but it's pretty simple - you're just storing all the enum entries and creating a lookup based on the name. That lets you be a little smarter if you need to, like making the lookup case-insensitive (which may or may not be a good thing, depending on why you're doing this)

The advantage here is that you're generating the lookup automatically - if you ever refactor the name of an enum constant, the string label will always match it (which you can get from the enum constant itself using its name property). Any "Dill" strings in your code will stay as "Dill" of course - that's the limitation of using hardcoded string lookups


The question really is, why do you want to do this? If it's pure data where no items need to be explicitly referenced in code, and it's all looked up at runtime, you should probably use a data class and a map, or something along those lines. If you do need to reference them as objects within the code at compile time (and trying to use HerbData."Dill".herbName implies you do) then an enum is a fairly easy way to let you do both


Declare a Data Class

data class HerbData (
val scientificName: String,
val dullThumbnail: Int
)

Initialize a muteable map and put data in it

val herbData = mutableMapOf<String, HerbData>()
herbData.put("Dill", HerbData("Anethum Graveolens", R.drawable.dill_thumbnail_attr))
herbData.put("Peppermint", HerbData("Mentha piperita", R.drawable.peppermint_thumbnail_attr))

You can now just

herbData["Dill"]?.scientificName