vetoable

This one is similar to observable; it accepts a function that will be invoked before each change of a property. But, vetoable can block the change, the function passed returns a Boolean, and if false is returned, the attempted change will be discarded and the property will have its old value. Let's say we have an Int property and allow only numbers less than 10:

var vetoable by Delegates.vetoable(1) { prop, oldVal, newVal ->
return@vetoable newVal <= 10
}

If we now try to assign 100 to it, the change will be blocked:

//change is blocked
vetoable = 100

And if the lambda returns true, the change will be successful:

//changed to 0
vetoable = 0
..................Content has been hidden....................

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