# How can I convert Any objects to objects of the correct types knowing only the names?

**URL:** <https://discuss.kotlinlang.org/t/how-can-i-convert-any-objects-to-objects-of-the-correct-types-knowing-only-the-names/7239>\
**Category:** Uncategorized\
**Created:** [March 29, 2018, 11:08am UTC](https://discuss.kotlinlang.org/t/how-can-i-convert-any-objects-to-objects-of-the-correct-types-knowing-only-the-names/7239 "2018-03-29T11:08:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![duckladydinh](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@duckladydinh](https://discuss.kotlinlang.org/u/duckladydinh)\
**Post date:** [March 29, 2018, 11:08am UTC](https://discuss.kotlinlang.org/t/how-can-i-convert-any-objects-to-objects-of-the-correct-types-knowing-only-the-names/7239/1 "2018-03-29T11:08:09Z")

</div>

Hello everyone,

I find the Kotlin Any object very very useful. However, when I get a result as an Any object “result”, I need to convert it to the correct type, for example, “CorrectTypeName”. I know we can do something like “result as CorrectTypeName”, but is there another way around to do it?

I am looking for a, for example, function like “fun convert(obj: Any, typename: String): T {…}” that will input an object of type Any and its correct type name as a string, and then output that object in its correct type. Is there any way to achieve it?

Thank you so much for your time and consideration.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [March 29, 2018, 11:16am UTC](https://discuss.kotlinlang.org/t/how-can-i-convert-any-objects-to-objects-of-the-correct-types-knowing-only-the-names/7239/2 "2018-03-29T11:16:43Z")

</div>

Not really. The problem is that you are trying to convert from information you only know at runtime to information you need to know at compile time. You could do something with predefined paths, where you for example only execute code if the obj is of type String or Foo, etc.

```kotlin
when(typename){
    "String" -> doSomethingWithString(obj as String)
    ....
}

```

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [March 29, 2018, 12:27pm UTC](https://discuss.kotlinlang.org/t/how-can-i-convert-any-objects-to-objects-of-the-correct-types-knowing-only-the-names/7239/3 "2018-03-29T12:27:40Z")

</div>

It is possible to cast an object to a type derived from its name at runtime.  
You can get java class from string using `Class.forName(...)` method and then use Java `Class::cast`(documentation [here](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#cast-java.lang.Object-)) to perform safe cast. But since the type information is accessible only in runtime, compiler won’t be able to use it. So the type will be correct, but you cant designate it as `T`,. It is sometimes useful, when you are using type from some external configuration and generating objects by reflections, but should be avoided otherwise.

Another possibility is the usage of kotlin reified inline function types:

```kotlin
inline fun <reified T: Any> Any.cast(): T{
  return this as T
}

val a: Any
val b = a cast<B>()

```

But I cant’t see how it is better then simple cast.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [March 29, 2018, 8:16pm UTC](https://discuss.kotlinlang.org/t/how-can-i-convert-any-objects-to-objects-of-the-correct-types-knowing-only-the-names/7239/4 "2018-03-29T20:16:46Z")

</div>

> [@darksnake](#):
>
> But I cant’t see how it is better then simple cast.

The compiler should convert this to a simple cast. I mean it is an inline reified function which by definition will be inlined 🙂

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [March 29, 2018, 8:31pm UTC](https://discuss.kotlinlang.org/t/how-can-i-convert-any-objects-to-objects-of-the-correct-types-knowing-only-the-names/7239/5 "2018-03-29T20:31:21Z")

</div>

I mean syntax. It is obvious that in the end any static cast will be reduced to language cast operation.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [March 29, 2018, 8:35pm UTC](https://discuss.kotlinlang.org/t/how-can-i-convert-any-objects-to-objects-of-the-correct-types-knowing-only-the-names/7239/6 "2018-03-29T20:35:48Z")

</div>

Maybe this syntax is better if the type of the cast can be inferred or if you prefere `any.cast<B>().doSomething()` over `(any as B).doSomething()`.

---

<div class="post-metadata">

**Author:** ![CNifeMe](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cnifeme/32/2746_2.png) [@CNifeMe](https://discuss.kotlinlang.org/u/CNifeMe)\
**Post date:** [April 5, 2018, 12:45am UTC](https://discuss.kotlinlang.org/t/how-can-i-convert-any-objects-to-objects-of-the-correct-types-knowing-only-the-names/7239/7 "2018-04-05T00:45:17Z")

</div>

It’s dangerous, what you need is generics.

---

<div class="post-metadata">

**Author:** ![duckladydinh](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@duckladydinh](https://discuss.kotlinlang.org/u/duckladydinh)\
**Post date:** [April 5, 2018, 6:25pm UTC](https://discuss.kotlinlang.org/t/how-can-i-convert-any-objects-to-objects-of-the-correct-types-knowing-only-the-names/7239/8 "2018-04-05T18:25:38Z")

</div>

Thank you all. Thanks to your answers, I finally know that it is impossible to do it nicely, so I have to need to struggle to find a way anymore. Thank you.
