I rewrite your connection a bit like this with a template:
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, if your MyStringEnum is defined as follows:
enum class MyStringEnum { FOO, BAR, BAZ }
You can call it like this:
println(MyStringEnum.values()[0].javaClass.join())
to get the exit "FOO, BAR, BAZ"
Since you are defining a union in a class, you need the actual class object to call it. Enum classes do not seem to work this way, but its specific enumerations can produce a class with javaClass . So this is the best I could come up with, and I think it fits the general spirit of your request. I donβt know if there is a more elegant way to achieve what you are trying to do for all enumeration classes like this.
EDIT: You can pull this up a bit:
fun Enum<*>.join(skipFirst: Int = 0, skipLast: Int = 0): String { return this.javaClass.join(skipFirst, skipLast) }
What you can call so:
println(MyStringEnum.values()[0].join())
source share