Here's an example from Delegated properties documentation.
import kotlin.reflect.KProperty
class Delegate {
operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
return "$thisRef, thank you for delegating '${property.name}' to me!"
}
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
println("$value has been assigned to '${property.name}' in $thisRef.")
}
}
This is because delegated properties are defined by convention. What this means is:
You can see the expansion of a delegated property further down the documentation page:
And one of the characteristics of a syntax that is defined by convention, is that (from the first link):
and also:
Just to give you more examples, the first link shows more examples of syntaxes that are defined by convention. Here are the corresponding operator functions related to them:
plus,compareToetcinvokeconventioninvokeplusAssign,minusAssignetciterator,hasNext,nextsetValue,getValuecomponent1,component2etcNotice that you need to put the word
operatoron all of those functions for the corresponding syntax to work. In other words, "operator" signifies that this function can be used in a syntax defined by convention.