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 in Kotlin is just a convenient and easy way to represent longer data types which might be frustrating otherwise to write at several places in the code. TypeAlias helps us define a short hand notation to represent the datatypes which once defined helps us use it as a placeholder for the data type for which it is defined.

I would be correlating this concept with the macro concept in C++ which we heavily use. Take a look at this code -

#define in C++

Here I have defined pb to be push_back() and vvi to be vector<vector<int>>, now if you have used them, then you must be knowing how much it helps us in writing concise code. And you can also check out the way I have used those macros in the subsequent code. TypeAlias performs a similar thing, now take a look at the below code -

typeAlias in Kotlin

Now in the above code, instead of writing the higher order function everytime as (Int) -> Int, we can define it using typeAlias and use it using the shortend name.

At the byte code level, there is no trace of TypeAlias, the JVM converts the shortend names into their original forms at the compile time and removes the TypeAlias keyword conveniently.

Do use TypeAlias in your day to day coding and reap its benefits. Happy coding.


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

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…

  • 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…

    1 条评论
  • 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 条评论

社区洞察

其他会员也浏览了