# Construction function using reified + when

**URL:** https://discuss.kotlinlang.org/t/construction-function-using-reified-when/1883
**Category:** Uncategorized
**Created:** [July 23, 2016, 4:53pm UTC](https://discuss.kotlinlang.org/t/construction-function-using-reified-when/1883 "2016-07-23T16:53:25Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![fab1an](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fab1an/32/1934_2.png) [@fab1an](https://discuss.kotlinlang.org/u/fab1an)
#### Post date: [July 23, 2016, 4:53pm UTC](https://discuss.kotlinlang.org/t/construction-function-using-reified-when/1883/1 "2016-07-23T16:53:25Z")

</div>

I’m trying out having a “factory” function like this. (The reason being that it’s used in generated code and the generator should stay simple).

```
inline fun <reified T: Any> get(str: String): T =
    when (T::class) {
        Uri::class -> Uri.parse(str) as T
        else -> throw IllegalStateException("don't know how to $str")
    }

val x = get<Uri>("www.example.com")

```

That works.

Now I also want to add `Enums`:

```
inline fun <reified T: Any> get(str: String): T =
    when (T::class) {
        Uri::class -> Uri.parse(str) as T
        Enum::class -> java.lang.Enum.valueOf(T::class.java, str)
        else -> throw IllegalStateException("don't know how to $str")
    }

```

This does not work:

![](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/2/215df3cfe1879f805408942a2a694a042fbdbcb6.png)

Any hints on such a use-case?

---

<div class="post-metadata">

### Author: ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)
#### Post date: [July 23, 2016, 5:41pm UTC](https://discuss.kotlinlang.org/t/construction-function-using-reified-when/1883/2 "2016-07-23T17:41:31Z")

</div>

We’re planning to address this in a future version of Kotlin: [Generic values() and valueOf() for enums by abreslav · Pull Request #37 · Kotlin/KEEP · GitHub](https://github.com/Kotlin/KEEP/pull/37)
