Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
Cómo usar el tipo de receptor de función con interfaces SAM en Kotlin

Vengo de Java y soy nuevo en Kotlin. Trato de entender cómo usar el tipo de receptor con lambdas especificadas como interfaces SAM funcionales.

Deja que el código hable por sí mismo.

 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" }

¿Cómo necesito cambiar el código para usar Delegator lambda con receptor dinámico?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede definir las funciones dentro del Delegator como función de extensión, de esta manera el receptor se pasa this a la 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() }

Alternativamente, puede simplemente definir el receptor dinámico con un typealias para lograr el mismo resultado.

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

En el sitio de uso, ambos enfoques tienen el mismo aspecto.

 fun main() { val result = invokeWithExtensionSamInterface("Foo") { length } println(result) val otherResult = invokeWithAliasSamInterface("Fizz") { length } println(otherResult) }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!