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\": \"[email protected]\"\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:[email protected]


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


3 comments :

  1. Machine Learning Projects for Final Year machine learning projects for final year

    Deep Learning Projects assist final year students with improving your applied Deep Learning skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include Deep Learning projects for final year into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Deep Learning Projects for Final Year even arrange a more significant compensation.

    Python Training in Chennai Python Training in Chennai Angular Training Project Centers in Chennai

    ReplyDelete
  2. Infycle Technologies offers couples for care and technology in addition to Python Training in Chennai, 100% of the internship class will be prepared. After completing the training, the participants will be sent to the upper MNCs interviews. Call 750633333 to get more information and get a free display.

    ReplyDelete

Your comments are valuable for us !!!