Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda