Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kotlin delegate property ,In a get() method how i can access the value?

Kotlin has delegated properties which is a very nice feature. But I am figuring out how to get and set the values. Let's say I want to get value of the property which is delegated. In a get() method how i can access the value?

Here's an example of how I have implemented:

class Example() {
    var p: String by DelegateExample()
}
class DelegateExample {
    operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
        return "${property.name} "
    }

  operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
    println("${value.trim()} '${property.name.toUpperCase()} '")
   }
}
fun delegate(): String {
    val e = Example()
    e.p = "NEW"
    return e.p
}

The main question I am unable to understand is, How can I set the value to the actual property on which the delegation class is assigned. When I assign "NEW" to property p, how can I store that value to the variable p or read that new value passed on to p with get? Am I missing something basic here? Any help will be much appreciated. Thanks in advance.

like image 259
Priya Avatar asked Dec 04 '17 13:12

Priya


People also ask

How do you use Kotlin delegate?

Kotlin supports “delegation” design pattern by introducing a new keyword “by”. Using this keyword or delegation methodology, Kotlin allows the derived class to access all the implemented public methods of an interface through a specific object.

What is delegate property in Kotlin?

Delegate properties in Kotlin are basically regular properties that delegate how they are read and written to another function (think of getters and setters). From an Android development perspective, this can be used in powerful ways.

What is lazy delegate Kotlin?

Lazy properties lazy() is a function that takes a lambda and returns an instance of Lazy<T> , which can serve as a delegate for implementing a lazy property. The first call to get() executes the lambda passed to lazy() and remembers the result. Subsequent calls to get() simply return the remembered result.


1 Answers

Just create property in delegate which will hold the value

class DelegateExample {

    private var value: String? = null        

    operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
        return value ?: throw IllegalStateException("Initalize me!")
    }

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
        this.value = value
    }
}

To clarify - delegates aren't values holder, they are handlers of get/set operations. You can take a look how it works under the hood if you decompile your Example class (Tools -> Kotlin -> Show Kotlin bytecode -> Decompile).

public final class Example {
   // $FF: synthetic field
   static final KProperty[] $$delegatedProperties = ...

   @NotNull
   private final DelegateExample p$delegate = new DelegateExample();

   @NotNull
   public final String getP() {
       return (String)this.p$delegate.getValue(this, $$delegatedProperties[0]);
   }

   public final void setP(@NotNull String var1) {
       Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
       this.p$delegate.setValue(this, $$delegatedProperties[0], var1);
   }
}

No magic here, just creating instance of the DelegateExample and its get/set method invoking

like image 182
hluhovskyi Avatar answered Oct 13 '22 18:10

hluhovskyi