Quantcast
Channel: How do I declare a variable of enum type in Kotlin? - Stack Overflow
Browsing all 5 articles
Browse latest View live

Answer by Sam for How do I declare a variable of enum type in Kotlin?

If you really do want to give the ability to construct arbitrary instances, use a value class instead of an enum.@JvmInline value class BitCount(val value: Int) { companion object { val x32 =...

View Article


Answer by Angel G. Olloqui for How do I declare a variable of enum type in...

What about:enum class BitCount constructor(val value : Int){ x32(32), x64(64); companion object { operator fun invoke(rawValue: Int) = BitCount.values().find { it.rawValue == rawValue } }}Then you can...

View Article

Answer by Jayson Minard for How do I declare a variable of enum type in Kotlin?

As stated in other answers, you can reference any value of the enum that exists by name, but not construct a new one. That does not prevent you from doing something similar to what you were trying...//...

View Article

Answer by user2235698 for How do I declare a variable of enum type in Kotlin?

Enum instances could be declared only inside enum class declaration.If you want to create new BitCount just add it as shown below:enum class BitCount public constructor(val value : Int){ x16(16),...

View Article

How do I declare a variable of enum type in Kotlin?

Following the documentation, I created an enum class:enum class BitCount public constructor(val value : Int){ x32(32), x64(64)}Then, I'm trying to declare a variable in some function:val bitCount :...

View Article

Browsing all 5 articles
Browse latest View live