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

237
Visualizações
How to use function receiver type with SAM interfaces in Kotlin

I'm coming from Java and am new to Kotlin. I try to understand how to use receiver type with lambdas specified as functional SAM interfaces.

Let the code speak for itself.

fun interface Delegator <T> {
    fun delegate(receiver: T)
}

fun <T> invokeWithDynamicReceiver(receiver: T, fn: T.() -> Unit) = receiver.fn()
fun <T> invokeWithSamInterface(receiver: T, fn: Delegator<T>) = fn.delegate(receiver)

fun dynamicReceiver() {
    invokeWithDynamicReceiver("Foo") { length } // Dynamic receiver
    invokeWithSamInterface("Foo") { it.length } // Can't bind receiver as "this"
}

How do I need to change the code to use the Delegator lambda with dynamic receiver?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can define the functions inside the Delegator as extension function, this way the receiver is passed as this to the lambda.

fun interface ExtensionDelegator <T, R> {
    fun T.delegate(): R
}

fun <T, R> invokeWithExtensionSamInterface(receiver: T, fn: ExtensionDelegator<T, R>): R =
    with(fn) { receiver.delegate() }

Alternatively, you can simply define the dynamic receiver with a typealias to achieve the same result.

typealias AliasDelegator<T, R> = T.() -> R

fun <T, R> invokeWithAliasSamInterface(receiver: T, fn: AliasDelegator<T, R>): R = fn.invoke(receiver)

On the use site, both approaches look the same.

fun main() {
    val result = invokeWithExtensionSamInterface("Foo") { length }
    println(result)

    val otherResult = invokeWithAliasSamInterface("Fizz") { length }
    println(otherResult)
}
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