Delegating Variables and Properties
We'll cover the following
In the examples so far, we focused on delegation at the class level. You may delegate get and set access to properties of objects and local variables too.
When you read a property or a local variable, internally Kotlin calls a getValue()
function. Likewise, when you update a property or a variable, it calls a setValue()
function. By providing as delegate an object with these two methods, you may intercept calls to read and write local variables and objects’ properties.
Delegating variables
You can intercept access, both read and write, to local variables and alter what is returned and where and how the data is stored. To illustrate this facility, let’s create a custom delegate to intercept access of String
variables.
Suppose we’re creating an application that takes users’ comments. The text they enter may be displayed to other users, and we definitely want to be polite. So let’s write a delegate that filters out an offensive word, like “stupid”.
Let’s look at a small script with no filtering:
Get hands-on with 1400+ tech skills courses.