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())
T : Enum<T>
. – Vergeboard