Type inference failed on function "enumSetOf"

In the following function

enum class TestEnum {A, B, C }

inline fun <reified T : java.lang.Enum<T>> enumSetOf(vararg elems: T) =
        java.util.EnumSet.noneOf(T::class.java).apply {
            addAll(elems)
        }


fun main(args: Array<String>) {
    java.util.EnumSet.noneOf(TestEnum::class.java)
    enumSetOf(TestEnum.A)
}

method noneOf has type inference failed “T isn’t a subtype of Enum”

It’s not without reason java.lang.Enum is highlighted in IDE with the warning:

This class shouldn’t be used in Kotlin. Use kotlin.Enum instead.

If you use kotlin.Enum (just Enum), enumSetOf is compiled without any error.

1 Like

Actually, I wonder why Kotlin doesn’t include such a method in it’s standard library yet, because EnumSet is designed especially for Enum types. mutableSetOf(...) could have a specialization for elements derived from Enum, which calls enumSetOf inside.