Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
Why do delegate class methods getValue and setValue need to marked with operator keyword?

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.")
    }
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

This is because delegated properties are defined by convention. What this means is:

[its] semantics are defined through syntactic expansion of one syntax form into another syntax form.

You can see the expansion of a delegated property further down the documentation page:

class C {
    var prop: Type by MyDelegate()
}

// this code is generated by the compiler instead:
class C {
    private val prop$delegate = MyDelegate()
    var prop: Type
        get() = prop$delegate.getValue(this, this::prop)
        set(value: Type) = prop$delegate.setValue(this, this::prop, value)
}

And one of the characteristics of a syntax that is defined by convention, is that (from the first link):

All call expressions that are produced by expansion are only allowed to use operator functions.

and also:

This expansion of a particular syntax form to a different piece of code is usually defined in the terms of operator functions.

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:

Syntax defined by convention Related operator functions
Arithmetic and comparison operators plus, compareTo etc
invoke convention invoke
Operator-form assignments plusAssign, minusAssign etc
For-loop statements iterator, hasNext, next
Delegated properties setValue, getValue
Destructuring declarations component1, component2 etc

Notice that you need to put the word operator on 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.

over 4 years ago · Santiago Trujillo Relatório

0

I'm not sure if I understand your question correctly, but we add operator to mark functions as not just regular functions, but functions designated to provide some specific functionality. This is not really limited to delegated properties - it is the same for all operators.

I think marking as operators have at least two benefits. First, it is explicit to the reader that the function can be used with some language feature. Second, it helps to avoid situations when we intended to provide some operator, but we made a mistake e.g. in parameter types, so it is silently ignored. operator immediately tells us whether the function signature is correct or not.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda