Type inference failed on function "enumSetOf"


#1

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”


#2

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.