Sobes.tech
Junior — Middle

What arguments and parameters does the getValue method of a delegate object accept?

sobes.tech AI

Answer from AI

The getValue method of a delegate object in Kotlin takes three parameters:

  1. thisRef — the object to which the delegate is applied (usually an instance of the class where the property is declared). The parameter's type matches the type of the property owner.
  2. property — an object of type KProperty<*>, which contains information about the property itself (e.g., the property name).

The method signature looks like this:

operator fun getValue(thisRef: Any?, property: KProperty<*>): T

where T is the type of the property's return value.

Example:

class Delegate {
    operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
        return "Value for property '${property.name}'"
    }
}

class Example {
    val prop: String by Delegate()
}

fun main() {
    val e = Example()
    println(e.prop) // Will output: Value for property 'prop'
}