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

364
Views
Equivalente de Kotlin para `@autoclosure` de Swift

Me pregunto si hay un equivalente a la función @autoclosure de Swift

Esencialmente, quiero poder crear un argumento en una función o constructor/inicializador que pueda tomar otra función que tome parámetros y ejecutarla:

 class Step(handler: () -> Unit) { init { handler() } } Step(aFunctionThatTakesParameters(parameter: String)) // <- Is there a way to get something like this working?

Como referencia, el código equivalente en Swift se ve así:

 struct Step { init(_ handler: @autoclosure () -> Void) { handler() } } Step(aFunctionThatTakesParameters(parameter: ""))
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Abrí la documentación de Swift para cierres automáticos en el enlace: https://docs.swift.org/swift-book/LanguageGuide/Closures.html#ID543

Eso hace referencia a este código:

 var customersInLine = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] print(customersInLine.count) // Prints "5" let customerProvider = { customersInLine.remove(at: 0) } print(customersInLine.count) // Prints "5" print("Now serving \(customerProvider())!") // Prints "Now serving Chris!" print(customersInLine.count) // Prints "4"

Luego escribí lo siguiente para traducir de Swift a Kotlin:

 val customersInLine = mutableListOf("Chris", "Alex", "Ema", "Barry", "Daniella") println(customersInLine.size) // Prints "5" val customerProvider = { customersInLine.removeAt(0) } println(customersInLine.size) // Prints "5" println("Now serving ${customerProvider()}!") // Prints "Now serving Chris!" println(customersInLine.size) // Prints "4"

Si bien esto no parece dar el resultado esperado que está buscando, podría hacer algo más en la línea de ...

 class Step (handler: () -> Unit) { init { handler() } } fun myParamFunction(a: String, b: String) { a + b } Step { myParamFunction("Hello ", "there") }

En este caso, lo mejor que puede hacer Kotlin es tomar una función lambda pura y llamar internamente a una función a la que se le han dado parámetros para llamar.

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!