https://kotlinlang.org logo
a

Animesh Sahu

12/30/2019, 1:52 PM
Is there way to add extensions to variables via delegation so that they may be reusable. Possible use case is to add a listener for the change in property, i know this can be done using external variable list containing lambdas and observable delegation but it is not reusable, as we need to create list for every variable we want to became observable, and it also doesn't reads/looks so good because you register observer lambda using member function of class not by the variable itself.
m

Marc Knaup

12/30/2019, 1:53 PM
a

Animesh Sahu

12/30/2019, 1:58 PM
Not exactly, i want to know that just like extension function is there a way to add some functions to specified properties.
(not a specific class)
m

Marc Knaup

12/30/2019, 1:59 PM
You can only add extensions to types, not to variables or functions. You can only mess around with class properties using property delegation.
As long as the property is delegated you could write something like this:
observe(someObject::someVariable) { … }
a

Animesh Sahu

12/30/2019, 2:03 PM
observe is not defined anywhere @Marc Knaup
m

Marc Knaup

12/30/2019, 2:03 PM
Yeah you have to write it
And the delegate
a

Animesh Sahu

12/30/2019, 2:03 PM
ohh ok
m

Marc Knaup

12/30/2019, 2:03 PM
Depends on what you want to achieve
Could also look like this:
someObject::someProperty.observe { … }
You’d have to write
.observe()
and
someProperty
must use a delegate you’ve written.
Maybe you can even put
someObject::someProperty
in a
Map
for later references, but you won’t get change notifications without a delegate
a

Animesh Sahu

12/30/2019, 2:05 PM
where do i write .observe(block: ()->Unit)?
to a variable
m

Marc Knaup

12/30/2019, 2:16 PM
It’s probably not a clean approach anyway and something like
val someProperty: Observable<Int>
should be used.
a

Animesh Sahu

12/30/2019, 2:23 PM
Even if its doesn't look so great but Its ok. A quick question What is use of
isAccessible = true
?