https://kotlinlang.org logo
#ktor
Title
# ktor
b

bod

02/14/2019, 11:20 AM
Hello, World! I have a pattern that happens a lot in our codebase: enums with a String key, and we need to get the enum value from the key, or a special “unknown” value. So what we do currently is something like:
Copy code
enum class MyEnum(val key: String) {
    FOO("foo"),
    BAR("bar"),
    UNKNOWN("unknown");

    companion object {
        fun getByKey(key: String?): MyEnum {
            return MyEnum.values().firstOrNull { it.key == key } ?: MyEnum.UNKNOWN
        }
    }
}
I’m looking for a way to not repeat this companion object for all our enums. Possibly a combination of an interface and a generic extension function? Any idea? 🙂
d

dany

02/14/2019, 11:48 AM
An extension method on Enum and having enum implements an interface like
interface LookableByKey { val key: String }
?
(btw not sure this is related to ktor...)
m

marstran

02/14/2019, 11:56 AM
Continuing with @dany's suggestion. You can do something like this:
Copy code
interface EnumCompanion<T : LookableByKey<*>> {
    val default: T
    fun values(): Array<T>
    fun getByKey(key: String?): T = values().firstOrNull { it.key == key } ?: default
}

interface LookableByKey<T> {
    val key: T
}

enum class MyEnum(override val key: String): LookableByKey<String> {
    FOO("foo"),
    BAR("bar"),
    UNKNOWN("unknown");

    companion object : EnumCompanion<MyEnum> {
        override fun values() = MyEnum.values()
        override val default = UNKNOWN
        
    }
}
b

bod

02/14/2019, 12:35 PM
wow, I’m very sorry, I posted this in the wrong channel! Was meaning to post in #general
but thanks a lot for the answer. @marstran It’s cool but still a bit a boilerplate per enum 🙂 I wonder if there could be an even better solution
I forgot companion object can inherit something, so that’s very cool
I hope you won’t find it rude that I’m gonna repost this to #general
d

dany

02/14/2019, 12:43 PM
why not having an extension on Enum something like
fun <T> Enum<T>.getByKey() : Enum<T>
?
m

marstran

02/14/2019, 12:45 PM
You will get some boilerplate per enum no matter what you do. The extension needs to know what the default-value is, and how to extract the key. And because
values()
is not part of the
Enum
class, you have to provide that function as well.
b

bod

02/14/2019, 12:46 PM
@danielm that was my first try but couldn’t achieve anything
m

marstran

02/14/2019, 12:46 PM
@dany Because that will require you to have an enum value from before. Like, you can only call it with
BAR.getByKey()
. It won't allow you to do
MyEnum.getByKey()
.
b

bod

02/14/2019, 12:47 PM
there’s also
enumValueOf
, I wonder if it could be of any help
d

dany

02/14/2019, 12:49 PM
indeed marius! missed that tks
m

marstran

02/14/2019, 12:51 PM
@bod Ah, right. There's also
enumValues
. That may make it simpler. 2 sec
Or not. The type parameter has to be reified and you can only reify type parameters on inline functions.
b

bod

02/14/2019, 12:55 PM
oh well 🙂
thanks a lot anyway!
m

marstran

02/14/2019, 12:56 PM
Oh, I managed to make it work.
Copy code
interface EnumCompanion<T> {
    val default: T
}

inline fun <reified T> EnumCompanion<T>.getByKey(key: String?): T
where T : Enum<T>, T : LookableByKey<*> {
    return enumValues<T>().find { it.key == key } ?: default
}

interface LookableByKey<T> {
    val key: T
}

enum class MyEnum(override val key: String): LookableByKey<String> {
    FOO("foo"),
    BAR("bar"),
    UNKNOWN("unknown");

    companion object : EnumCompanion<MyEnum> {
        override val default = UNKNOWN
    }
}
👍 1
b

bod

02/14/2019, 12:58 PM
wow, not that’s definitely cool!
never seen
where
used before, I guess that’ll be a first for me 😉
m

marstran

02/14/2019, 1:00 PM
Hehe, you need it when the type parameter has more than one constraint.
👍 1
@bod You can actually get rid of the
LookableByKey
interface as well if you change the
EnumCompanion
to this:
Copy code
interface EnumCompanion<T, K> {
    fun T.key(): K
    val default: T
}
b

bod

02/14/2019, 2:01 PM
even better! 👍