How it works...

We are delegating the var temperature variable to the result of the Delegates.observable() function, which returns an instance of the ReadWriteProperty class. That fact makes it possible to declare temperature as a mutable variable. The observe() function takes two arguments—the initial value, and an instance of the hook function that is going to be invoked on every change made to the delegated variable. In our case, we are instantiating the function as the lambda block, which is supposed to print the new temperature value to the console.

Let's test how our implementation is going to work. We are going to modify the value of the temperature directly a couple of times:

temperature = 10
temperature = 11
temperature = 12
temperature = 30

As the result, we get the following output:

Current temperature: 10
Current temperature: 11
Current temperature: 12
Current temperature: 30

On each change of the temperature value, the listener function is being invoked with the previous and new values of the property passed to its parameters.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.50.198