How to check whether a type is an enum in Kotlin?

kotlin enum tostring
kotlin enum with int value
kotlin enum ordinal
kotlin enum default value
kotlin enumset
kotlin check if enum contains value
kotlin enum naming convention
kotlin: enum types cannot be instantiated

At runtime, I am trying to verify whether a particular KClass<out Any> is an enum type or not.

What is the best way to do so? Can this be done without relying on a particular runtime (e.g., JVM or JS)?

fun isEnum( type: KClass<out Any> ): Boolean
    ... ?

Also a JVM-only solution, but a shorter one, using isSubClassOf:

fun isEnum(type: KClass<out Any>) = type.isSubclassOf(Enum::class)

Enum Classes, The most basic usage of enum classes is implementing type-safe enums: derive from a class), providing either a single interface members implementation for  Porting this over to Kotlin with a simple enum leads me to code that looks like this: enum class Type(val value: Int) { A(1), B(2), C(3); companion object { val map: MutableMap<Int, Type> = HashMap() init { for (i in Type.values()) { map[i.value] = i } } fun fromInt(type: Int?):

JVM specific

None of the solutions here where working for me (I had a KType) so I came up with another approach. Here is a solution for converting the KClass to a KType and then checking if the KType is an enum.

fun isEnum(kClass: KClass<out Any> ): Boolean {
    val kType :KType = kClass::class.createType()
    return (kType.javaType  as Class<*>).isEnum

Enum, See the Kotlin language documentation for more information on enum classes. Represents variance applied to a type parameter on the declaration site  Enum entries cannot contain nested types other than inner classes (deprecated in Kotlin 1.2). Implementing Interfaces in Enum Classes. An enum class may implement an interface (but not derive from a class), providing either a single interface members implementation for all of the entries, or separate ones for each entry within its anonymous class.

The following seems to work for JVM, relying on the qualified type name.

fun isEnum( type: Kclass<out Any> ): Boolean
    return type.supertypes.any { t ->
        (t.classifier as KClass<out Any>).qualifiedName == "kotlin.Enum" }

However, this does not work for JS since KClass::supertypes is not available for that runtime.

Type Checks and Casts: 'is' and 'as', We can check whether an object conforms to a given type at runtime by using the is The compiler is smart enough to know a cast to be safe if a negative check  class Enum < T : Enum < T > > : Comparable < Enum < T > > The common base class of all enum classes. See the Kotlin language documentation for more information on enum classes.

Working with Enums in Kotlin, For Kotlin beginners, check out this article on Kotlin basics Let's define an enum as having three constants describing credit card types: ? Type erasure and generic type checks. Kotlin ensures type safety of operations involving generics at compile time, while, at runtime, instances of generic types hold no information about their actual type arguments. For example, List<Foo> is erased to just List<*>.

Kotlin Enum Class, We'll be discussing everything you need to know about kotlin enum class at length. Table of Enumerations in Kotlin are data types that hold a set of constants. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Kotlin enum classes have "static" function valueOf to

Enum Classes in Kotlin, In programming, sometimes there arises a need for a type to have only certain As in Java and in other programming languages, Kotlin enum classes has See your article appearing on the GeeksforGeeks main page and help other Geeks. public override bool CanConvert(Type objectType) { } I am trying to figure out how to determine if objectType is an Enum and return true so that the converter knows it can convert this object. Since I have many Enum's, I cannot explicitly check each one so I was hoping for a more generic way of accomplishing this.