Jen
Jen

Reputation: 1338

Where is the Enum::valueOf defined?

When I define an enum class in Kotlin

enum class Answer {
    YES,
    NO
}

It has a valueOf(value: String) attached to it.

val doYouWantBeerOrCoffee = Answer.valueOf("YES") // Answer.YES

But where is this function actually defined? It is definitely not in the Enum.Kt and using Idea's Go to Implementation tool only takes me back to my Answer enum definition.

Upvotes: 2

Views: 302

Answers (2)

Alexey Romanov
Alexey Romanov

Reputation: 170745

It's generated by the compiler. That's what "synthetic" means in

Enum classes in Kotlin have synthetic methods allowing to list the defined enum constants and to get an enum constant by its name.

If you decompile Answer.class you'll see it, but it isn't written as Kotlin (or Java) source code anywhere.

Upvotes: 1

ish
ish

Reputation: 171

This method is a part of JDK, and defined in Enum.java class. Which is the common base class of all Java language enumeration types. Kotlin uses the same class for enums

Upvotes: 0

Related Questions