geg
geg

Reputation: 4785

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

I'm trying to extend enum classes of type String with the following function but am unable to use it at the call site like so:

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

MyStringEnum.join(1, 1);

What am I doing wrong here?

Upvotes: 24

Views: 27502

Answers (4)

Miha_x64
Miha_x64

Reputation: 6363

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>()

Upvotes: 14

Kirill Rakhman
Kirill Rakhman

Reputation: 43811

@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()
}

Upvotes: 8

Ruslan
Ruslan

Reputation: 14620

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

Upvotes: 22

Doug Stevenson
Doug Stevenson

Reputation: 317382

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())

Upvotes: 4

Related Questions