Delegates in Kotlin

Delegates in Kotlin

Today I will be writing about my understanding regarding the delegates in Kotlin.

The english meaning of Delegation simply means transferring the responsibility of doing something to someone else.

In context of Koltin, it means passing the responsibility of performing one task to another object which is called the Delegate, since it is the one performing the real task.

While this thing is achievable by using higherOrder functions in Kotlin, yet we generally use delegates to reduce heavy boilder plate code which we would have otherwise written using inheritance concepts.

I will illustrate two popular delegations using the below code -

Class delegation

Here we have two classes namely JuniorCoder and SeniorCoder, where the first category can only write code, but the second category can write and review code too.

This is the power of Delegation or the by keyword, instead of making SeniorCoder class implement the Coder interface we have delegated the common tasks to the JuniorCoder class by writing this single line -

class SeniorCoder(val name: String): Coder by JuniorCoder(name)        

Check the logs for the above code -

Class delegation logs

Now let us understand the variable delegation, where we delegate the variable initialization to lazy which initializes it when the variable is first accessed. Take a look at the below code -

Variable delegation

Here, we are initializing the varibale myString using lazy, and the result is that we can only see the string "Initializing the string" printed single time, at the time of first access. And after that no matter how many times we access it, it is just readonly with the value as "Sid". Check the below logs -

Variable delegation logs

I hope we learnt something from this explaination on delegates in Kotlin. Thanks for reading. 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…

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

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

社区洞察

其他会员也浏览了