Last Updated: September 19, 2018

Kotlin Reified Type

Description: In this post I'm gonna explain what is koltin reified types? with sample examples and explanation about its need and requirement in our application

If you are new to Koltin I recommend Switch from Java to Kotlin

So let's get started.



Why we need to know reified types?

Consider an below example in 'Java' which takes JSON and convert that into equivalent model class using Gson library (you can use any other parsing library like Jackson etc).

public T createModelFromClass(String jsonString) {
    return new Gson().fromJson(jsonString, T.class);
}

Now, if you try this way it will show an error saying "Cannot select from type variable". Because of "type erasure" in java generics which erased the type at run time.
Let's fix this!
public T createModelFromClass(String jsonString, Class<T> classT) {
    return new Gson().fromJson(jsonString, classT);
}

Consider the same example in kotlin

fun <T> createModelFromClass(jsonString: String): T {
    return Gson().fromJson(jsonString, T::class.java)
}
Now, if you try this way it will show an error saying ' Cannot use 'T' as reified type parameter. Use a class instead.

Because like in java the Generics 'T' get erased at run time.

Let's fix this!

fun <T: Any> createModelFromClass(jsonString: String, tClass: KClass<T>): T {
    return Gson().fromJson(jsonString, tClass.java)
}

Here comes the need!

In java and koltin example above, we need to pass the 'Class' so that the bytecode of the class could be generated at runtime.

Introducing reified type!
Reified in combination with inline function generates the bytecode of the class at runtime. So the overhead of passing <KClass> would be avoided within function.

How to use reified type?

Let's modify the above example of koltin using reified type




inline fun <reified T : Any> createModelFromClass(jsonString: String): T {
    return Gson().fromJson(jsonString, T::class.java)
}

Note: This functions with reified type are not callable from Java code.

Sample within the activity

class MainActivity : AppCompatActivity() {

    val USER_JSON = "{\"id\": 1000,\n" +
            " \"name\": \"nitesh tiwari\",\n" +
            " \"email\": \"nitiwari.dev@gmail.com\"\n" +
            "}"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val userModel = createModelFromClass<User>(USER_JSON)
        Log.d("Reified", "\nName: " + userModel.name + "\nId:" + userModel.id + "\nEmail:" + userModel.email)
    }


    data class User(var id: Int, var name: String?, var email: String?)

    inline fun <reified T : Any> createModelFromClass(jsonString: String): T {
        return Gson().fromJson(jsonString, T::class.java)
    }
}


Output:

Name: nitesh tiwari
Id:1000
Email:nitiwari.dev@gmail.com


Bingo! We are done. We have just avoided the overhead of passing the 'KClass' in method constructor to generate its byte code at runtime.


2 comments :

  1. Enroll in the best Autocad institute in Delhi and Noida that is High technologies solutions. Web at HTS provides Real life case studies, live projects, Assignments, free demo class and placement assistance. Call us for more details at 9311002620 or visit our website.
    autocad training institute in Delhi
    autocad training institute in Noida

    ReplyDelete

Your comments are valuable for us !!!