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 -

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 -

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 -

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

8 个月

Useful tips

要查看或添加评论,请登录

Siddhant Mishra的更多文章

  • SuspendCoroutine

    SuspendCoroutine

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

  • findViewById - Internals

    findViewById - Internals

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

    2 条评论
  • Lambda functions

    Lambda functions

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

    1 条评论
  • Exceptions in Coroutines

    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

    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

    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

    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

    Delegates in Kotlin

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

  • TypeAlias in Kotlin

    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…

  • Jetpack Compose - Interesting features to dive deeper into.

    Jetpack Compose - Interesting features to dive deeper into.

    Recently I got to migrate my sample project from the conventional XML to Jetpack Compose, and I would like to list down…

    3 条评论

社区洞察

其他会员也浏览了