Quantcast
Channel: How do I create an enum from an Int in Kotlin? - Stack Overflow
Browsing all 13 articles
Browse latest View live
↧

Answer by heyshowshana for How do I create an enum from an Int in Kotlin?

I think another solution might be clearer, it's possible to use a String to initialize the enum.It's better human-readable and can be seen right away what the enum value is.For example:enum class...

View Article


Answer by Ji Fang for How do I create an enum from an Int in Kotlin?

Protocol orientated way with type-safetyinterface RawRepresentable<T> { val rawValue: T}inline fun <reified E, T> valueOf(value: T): E? where E : Enum<E>, E: RawRepresentable<T>...

View Article

Answer by Rybin Peter for How do I create an enum from an Int in Kotlin?

Another option...enum class Types(val code: Int) { FOO(1), BAR(2), FOO_BAR(3); companion object { val map = values().associate { it.code to it } // Get Type by code with check existing codes and...

View Article

Answer by Mahozad for How do I create an enum from an Int in Kotlin?

This is for anyone looking for getting the enum from its ordinal or index integer.enum class MyEnum { RED, GREEN, BLUE }MyEnum.values()[1] // GREENAnother solution and its variations:inline fun...

View Article

Answer by Stoica Mircea for How do I create an enum from an Int in Kotlin?

If you hate declaring for each enum type a companion object{ ... } to achieve EMotorcycleType.fromInt(...). Here's a solution for you.EnumCaster object:object EnumCaster { inline fun <reified E :...

View Article


Answer by Michal Zhradnk Nono3551 for How do I create an enum from an Int in...

If you are using integer value only to maintain order, which you need to access correct value, then you don't need any extra code. You can use build in value ordinal. Ordinal represents position of...

View Article

Answer by Hani for How do I create an enum from an Int in Kotlin?

try this...companion object{ fun FromInt(v:Int):Type{ return Type::class.java.constructors[0].newInstance(v) as Type }}

View Article

Answer by axblount for How do I create an enum from an Int in Kotlin?

I would build the 'reverse' map ahead of time. Probably not a big improvement, but also not much code.enum class Test(val value: Int) { A(1), B(2); companion object { val reverseValues: Map<Int,...

View Article


Answer by Joffrey for How do I create an enum from an Int in Kotlin?

It really depends on what you actually want to do.If you need a specific hardcoded enum value, then you can directly use Types.FOOIf you are receiving the value dynamically from somewhere else in your...

View Article


Answer by Zoe - Save the data dump for How do I create an enum from an Int in...

Enum#valueOf is based on name. Which means in order to use that, you'd need to use valueof("FOO"). The valueof method consequently takes a String, which explains the error. A String isn't an Int, and...

View Article

Answer by Geno Chen for How do I create an enum from an Int in Kotlin?

A naive way can be:enum class Types(val value: Int) { FOO(1), BAR(2), FOO_BAR(3); companion object { fun valueOf(value: Int) = Types.values().find { it.value == value } }}Then you can use var bar =...

View Article

Answer by Francesc for How do I create an enum from an Int in Kotlin?

enum class Types(val value: Int) { FOO(1), BAR(2), FOO_BAR(3); companion object { fun fromInt(value: Int) = Types.values().first { it.value == value } }}You may want to add a safety check for the range...

View Article

How do I create an enum from an Int in Kotlin?

I have this enum:enum class Types(val value: Int) { FOO(1) BAR(2) FOO_BAR(3)}How do I create an instance of that enum using an Int?I tried doing something like this:val type = Types.valueOf(1)And I get...

View Article

Browsing all 13 articles
Browse latest View live




<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>