Kotlin: How to extend the enum class with an extension function

@IRus' answer is correct but you don't have to use reflection. For every enum class, a values() method is automatically generated by the compiler. This method returns an array containing all the entries. We can make the extension function operate directly on this array like this:

fun <T : Enum<*>> Array<T>.join(skipFirst: Int = 0, skipLast: Int = 0)
        = drop(skipFirst)
        .dropLast(skipLast)
        .map { e -> e.name }
        .joinToString()

And call it like this:

fun main(args: Array<String>) {
    Test.values().join()
}

I suggest following solution:

fun <T : Enum<*>> KClass<T>.join(skipFirst: Int = 0, skipLast: Int = 0): String {
    return this.java
            .enumConstants
            .drop(skipFirst)
            .dropLast(skipLast)
            .map { e -> e.name }
            .joinToString()
}

Instead of attaching extension function to Class, i attached it to KotlinClass.

Now, you can simply use it:

enum class Test {ONE, TWO, THREE }

fun main(args: Array<String>) {
    println(Test::class.join())
}
// ONE, TWO, THREE

Use of ::class is a nasty workaround. I suggest you to look at enumValues<E> and enumValueOf<E> from stdlib and do the same way:

inline fun <reified E : Enum<E>> joinValuesOf(skipFirst: Int = 0, skipLast: Int = 0): String =
        enumValues<E>().join(skipFirst, skipLast)

@PublishedApi
internal fun Array<out Enum<*>>.join(skipFirst: Int, skipLast: Int): String =
        asList()
                .subList(skipFirst, size - skipLast)
                .joinToString(transform = Enum<*>::name)

Usage: joinValuesOf<Thread.State>()


I'll rewrite your join slightly like this with a wildcard:

fun <T: Enum<*>> Class<T>.join(skipFirst: Int = 0, skipLast: Int = 0): String {
    return this.enumConstants
            .drop(skipFirst)
            .dropLast(skipLast)
            .map { e -> e.name }
            .joinToString()
}

Then, assuming your MyStringEnum is defined like this:

enum class MyStringEnum { FOO, BAR, BAZ }

You can call it like this:

println(MyStringEnum.values()[0].javaClass.join())

to get output "FOO, BAR, BAZ"

Since you're defining join on Class, you need an actual Class object to call it on. Enum classes apparently don't work like that, but its defined enums can yield a Class with javaClass. So this is the best I could come up with that I think meets the general spirit of your request. I don't know if there is a more elegant way to achieve what you're trying to do for all enum classes like this.

EDIT: You can tighten this up a little bit more with this:

fun Enum<*>.join(skipFirst: Int = 0, skipLast: Int = 0): String {
    return this.javaClass.join(skipFirst, skipLast)
}

Which lets you call like this:

println(MyStringEnum.values()[0].join())