Mateusz Teteruk’s Post

View profile for Mateusz Teteruk

Pokazuję, jak wykorzystuję AI w codzienności i jako programista | Android Engineer

4 Kotlin Property Delegates You Must Know👇🏻 Yesterday, we covered the delegation pattern in Kotlin. You know how to use it with classes. Today, let's focus on properties.  It's far more interesting! Property delegation in Kotlin allows you to delegate the responsibility of getting and setting a value of a property to a separate class. As you know, Kotlin has a lot to offer in terms of built-in solutions.  Let's go through some of them one by one. -> Lazy Initialization One of the most common use cases for property delegation is lazy initialization using the `lazy` function. This is particularly useful when the property initialization is expensive, and you want to delay it until the property is actually accessed. -> Observable Properties Another scenario is when you want to observe changes to a property, for example, to update the UI or log changes. Kotlin's standard library provides a `Delegates.observable()` that allows you to add a listener that gets notified every time the property value changes. -> Intercept Observable Properties Quite similar to `observable`, but with the possibility to intercept new value assignments, is a `vetoable` delegate. The handler passed to `vetoable` will be called before the assignment of a new property value. -> Storing Properties in a Map Sometimes, you might want to store properties of an object in a map rather than as separate fields. This can be particularly useful in dynamic situations like parsing JSON or working with scripting languages. Kotlin supports this pattern out of the box as well. We can probably find a few more examples, but let's wrap it up for now! Why should you use it? It's all about keeping your code clean and focused! Each class sticks to its responsibilities. Plus, Kotlin's syntax for delegation is super elegant, don't you think? What's your favourite delegate? I'd choose `lazy` for sure! Let me know in the comments! PS If you like such content, follow me for more. #Kotlin #Android

Mateusz Teteruk

Pokazuję, jak wykorzystuję AI w codzienności i jako programista | Android Engineer

1y

Post from yesterday, Delegation Pattern 101: https://www.linkedin.com/feed/update/urn:li:activity:7181179631058788352/

Like
Reply
Harshit Singhal

📱 Android Dev | Java/Kotlin ☕ | GenAI Dev🧑💻 |Coding Enthusiast 💡 | Freelance Coder 💼 #CodeInPublic

1y

This is a great post that explains property delegation in Kotlin very clearly. I especially liked the explanation of lazy initialization and observable properties. Thanks for sharing!

Eran Boudjnah

Lead Android Developer • Available Now for roles in UK/EU • Author: Clean Architecture for Android, There is no I in IT • Kotlin

1y

I see some really worrying patterns there. Even though the language supports vetoable, it's not a great idea. Rejecting assignments implicitly and silently can lead to some annoying bugs. And using a map for fields, while interesting, loses the type safety. Why would we risk that?

Ankit Kumar

Software Engineer Android

1y

Informative!, I have one doubt Instead of `Observable delegates` can't we use `setters` it looks more familiar Do you see any challenges or drawbacks when using setters?

some of them are quite new to me

Like
Reply
Nicolas Machado

Genexus Developer | Sr Android Engineer | Kotlin | Jetpack Compose | Clean Architecture | MVVM | Mobile Developer | Java Developer | RPG | LegaSuite

1y

what about "by" delagate?

Like
Reply
Osvel Alvarez Jacomino

Senior Android Engineer | Kotlin & Java | Jetpack Compose

1y

Those are really useful!

akarsh mittal

Software Engineer @DTDL || Android || Kotlin || Java

1y

A simple custom Setter could do the work of Observable Properties and intercepting the changes. But mostly I use them for debugging purposes.

Like
Reply
See more comments

To view or add a comment, sign in

Explore topics