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

124
Views
Agregue la función de extensión en kotlin a todas las clases

¿Es posible agregar la función de extensión a todas las clases? Estaba pensando en agregarlo a alguna clase base común como Object. ¿Es posible?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Con Kotlin, Any es el supertipo como Object para Java.

 fun Any.myExtensionFunction() { // ... }

Y si también quieres admitir null-receiver:

 fun Any?.myExtensionFunction() { // ... }
over 4 years ago · Santiago Trujillo Report

0

Depende de si desea utilizar el tipo del objeto en la firma (ya sea en otro argumento o en el tipo de retorno). Si no, use Any(?) como dice la respuesta de Kevin Robatel; pero si lo hace, necesita usar genéricos, por ejemplo (de la biblioteca estándar)

 inline fun <T, R> T.run(block: T.() -> R): R inline fun <T> T.takeIf(predicate: (T) -> Boolean): T?

etc.

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!