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() .
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 .
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.
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.