Reified in Kotlin

Reified in Kotlin

Today I will be writing about the type erasure in Java and Kotlin and its solution.

The concept of type erasure stems from the fact that for previous versions of Java there were no concept of Generics(popularly represented as <T> in classes's, functions's definitions), hence after introduction of newer Java versions and to keep resonance with the older versions they did not retain the datatype at runtime this is what we call type erasure.

And Kotlin also started to respect the type erasure to let developers use the old java libraries seamlessly.

Now for understaning what exactly type erasure is, check this below code -

Article content
Type erasure

In the above function I am trying to check the type of a variable, I am passing. The cache is that, the type checking is being done at the runtime where the type of something is compared with T, and at runtime, the JVM can not infer the type of the generic type(T) this is what exactly is type erasure and at the compile time itself it gives us the suggestion -

Article content
Importance of reified

Now in the suggestion it tells us to change the function to inline(yes reified only works with inline function) and add the reified keyword before T(our generic data type).

Now the correct way to check the type looks something like this -

Article content
Reified plus inline

Now the function gives us correct answers, as it remembers the String and Boolean as the generic data types for the two method calls.

Follow me to read about such simplified concepts. Happy Coding



Dinesh Jangu

Software engineer | PHP | Laravel | jQuery | Javascript | React js | git | WordPress | Bootstrap | Tailwind | devops

10mo

Useful tips

To view or add a comment, sign in

More articles by Siddhant Mishra

  • Dispachers.IO vs Dispathers.Default

    While coroutines elegantly enable us to write asynchronous code in a synchronous fashion in Android development, there…

  • SuspendCoroutine

    Today I will be writing about the usage of an important concept in Android namely suspendCoroutine. By definition…

  • findViewById - Internals

    Hope everyone is doing well. Today I will be writing about the internal working of findViewById in Android.

    1 Comment
  • Lambda functions

    Today I will be writing about two different yet intriguing way to write the lambda functions in Kotlin. Conventionally…

    1 Comment
  • Exceptions in Coroutines

    Hi everyone, today I will be writing about exceptions in Coroutine and how to handle them gracefully. First let us see…

  • Trie data structure

    Today I will write about a magical data structure namely Trie. When we talk of storing and retriving some information…

  • Vertical Order traversal in binary tree

    We generally talk about Inorder, PreOrder, PostOrder and LevelOrder traversals in binary tree, but we generally do not…

  • LCA in Binary Tree

    Today I will be writing about the LCA(Least Common Ancestor) for a binary tree. LCA refers to the node in the binary…

  • Delegates in Kotlin

    Today I will be writing about my understanding regarding the delegates in Kotlin. The english meaning of Delegation…

  • TypeAlias in Kotlin

    Today I will be writing about TypeAlias in Kotlin and how do I understand and use it in my day to day coding. Typealias…

Insights from the community

Others also viewed

Explore topics