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

174
Views
¿Alguna diferencia entre String.getOrElse() y String.elementAtOrElse()?

Como dice el título:

¿Hay alguna diferencia entre String.getOrElse() y String.elementAtOrElse() ? Desde un punto de vista funcional parecen completamente idénticos, ¿tal vez alguna diferencia de rendimiento?

Las mismas cuentas de preguntas para String.getOrNull() y String.elementAtOrNull() .

over 4 years ago · Hanz Gallego
3 answers
Answer question

0

Los mismos enlaces que incluyó en su pregunta le permiten ver el código fuente de cada implementación que le dice que no, no hay diferencia.

De hecho, elementAtOrNull literalmente solo llama a getOrNull .

over 4 years ago · Hanz Gallego Report

0

Al observar la implementación en https://github.com/JetBrains/kotlin/blame/master/libraries/stdlib/common/src/generated/_Strings.kt , se ven idénticos.

 /** * Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this char sequence. * * @sample samples.collections.Collections.Elements.elementAtOrElse */ @kotlin.internal.InlineOnly public inline fun CharSequence.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) }
 /** * Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this char sequence. */ @kotlin.internal.InlineOnly public inline fun CharSequence.getOrElse(index: Int, defaultValue: (Int) -> Char): Char { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) }

Espero que alguien más pueda proporcionar detalles sobre la historia de esto.

over 4 years ago · Hanz Gallego Report

0

Para explicar el por qué:

Del problema que los agregó en https://youtrack.jetbrains.com/issue/KT-6952 , parece que elementAtOrElse() se agregó primero y se nombró así por compatibilidad con Iterable s, mientras que getOrElse() se agregó más tarde por compatibilidad. con List s.

over 4 years ago · Hanz Gallego 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!