# Using enums generically

**URL:** https://discuss.kotlinlang.org/t/using-enums-generically/317
**Category:** Uncategorized
**Created:** [March 5, 2015, 7:43pm UTC](https://discuss.kotlinlang.org/t/using-enums-generically/317 "2015-03-05T19:43:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ajselvig](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@ajselvig](https://discuss.kotlinlang.org/u/ajselvig)
#### Post date: [March 5, 2015, 7:43pm UTC](https://discuss.kotlinlang.org/t/using-enums-generically/317/1 "2015-03-05T19:43:04Z")

</div>

I'm developing a system that uses delegated properties to perform some operations on property values. For example, I have something like this for a string:

```
class StringField(var value : String) { &nbsp;&nbsp;fun get(thisRef: Any?, prop: PropertyMetadata): String { &nbsp;&nbsp;return value &nbsp;&nbsp;}

&nbsp;&nbsp;fun set(thisRef: Any?, prop: PropertyMetadata, newValue: String) {  
&nbsp;&nbsp;// do something with the value  
  &nbsp;&nbsp;value = newValue  
&nbsp;&nbsp;}  
}
```

So that I can define my properties like:

var name by StringField(“default”)

I’d like to do the same thing with enums without having to define a new delegate for each enum type. I couldn’t figure out how to do it with actual enum types, but I came up with a slightly convoluted way with my own custom class:

```
open class StringEnum(var value : String = "")

open class StringEnumObject\<T : StringEnum\>() {  
&nbsp;&nbsp;val values = ArrayList\<T\>()

 

&nbsp;&nbsp;inline fun \<reified Tnew : T\>value(s : String) : T {  
&nbsp;&nbsp;val inst = javaClass\<Tnew\>().newInstance()  
&nbsp;&nbsp;inst.value = s  
&nbsp;&nbsp;values.add(inst)  
&nbsp;&nbsp;return inst  
&nbsp;&nbsp;}  
}
```

```

```

```
class StringEnumField\<T : StringEnum\>(var value : T) {  
  &nbsp;&nbsp;fun get(thisRef: Any?, prop: PropertyMetadata): T {  
&nbsp;&nbsp;return value  
  &nbsp;&nbsp;}

 

&nbsp;&nbsp;fun set(thisRef: Any?, prop: PropertyMetadata, newValue: T) {  
&nbsp;&nbsp;// do something with the value  
  &nbsp;&nbsp;value = newValue  
&nbsp;&nbsp;}  
}
```

This way, the enums can be declared and used like this:

```
class Status(value : String = "") : StringEnum(value) { &nbsp;&nbsp;class object : StringEnumObject\<Status\>() { &nbsp;&nbsp;val open = value\<Status\>("open") &nbsp;&nbsp;val closed = value\<Status\>("closed") &nbsp;&nbsp;} }
```

val status by StringEnumField(Status.open)

This is all fine and good, but it’s a bit verbose and I feel like I’m not using the type system properly. So the question: is there a way to make a generic property delegate that works on any enum?

---

<div class="post-metadata">

### Author: ![ajselvig](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@ajselvig](https://discuss.kotlinlang.org/u/ajselvig)
#### Post date: [March 5, 2015, 9:19pm UTC](https://discuss.kotlinlang.org/t/using-enums-generically/317/2 "2015-03-05T21:19:41Z")

</div>

More specifically, I _am_ able to make a generic field that uses enums, but am not able to instantiate new values. This is the core functionality I'm looking for (the abillity to persist these properties to/from JSON and a database).

```
class EnumField\<T : Enum\<T\>\>(var value : T) { &nbsp;&nbsp;fun get(thisRef: Any?, prop: PropertyMetadata): T { &nbsp;&nbsp;return value &nbsp;&nbsp;}

&nbsp;&nbsp;fun set(thisRef: Any?, prop: PropertyMetadata, newValue: T) {  
  &nbsp;&nbsp;value = newValue  
&nbsp;&nbsp;}  
&nbsp;&nbsp;  
&nbsp;&nbsp;fun fromString(raw : String) {  
&nbsp;&nbsp;value = // create a new value from raw  
  &nbsp;&nbsp;}  
}
```

So, I can’t figure out how to implement the function fromString().

---

<div class="post-metadata">

### Author: ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)
#### Post date: [March 6, 2015, 1:48pm UTC](https://discuss.kotlinlang.org/t/using-enums-generically/317/3 "2015-03-06T13:48:40Z")

</div>

Why can't you take the class of `value` through reflection and call `Enum.valueOf(thatClass, stringValue)`? Or am I getting your use case wrong?

---

<div class="post-metadata">

### Author: ![ajselvig](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@ajselvig](https://discuss.kotlinlang.org/u/ajselvig)
#### Post date: [March 6, 2015, 4:58pm UTC](https://discuss.kotlinlang.org/t/using-enums-generically/317/4 "2015-03-06T16:58:28Z")

</div>

Okay, you're right. I had tried this initially but it wasn't working because it seems like the Kotlin Enum class hides the java one? So I ended up needing to fully qualifiy it:

java.lang.Enum.valueOf(value.javaClass, s)

Thanks!
