正如標題所說:
有什么區別String.getOrElse()和String.elementAtOrElse()?從功能的角度來看,它們似乎完全相同,也許有些性能差異?
同樣的問題說明String.getOrNull()和String.elementAtOrNull()。
uj5u.com熱心網友回復:
查看https://github.com/JetBrains/kotlin/blame/master/libraries/stdlib/common/src/generated/_Strings.kt中的實作,它們看起來相同。
/**
* 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)
}
我希望其他人可以提供有關此歷史的詳細資訊。
uj5u.com熱心網友回復:
您在問題中包含的鏈接允許您查看每個實作的源代碼,它告訴您,不,沒有區別。
事實上,elementAtOrNull字面上只是呼叫getOrNull。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371510.html
標籤:科特林
上一篇:如何在沒有背景的情況下匯入pngandroidstudio
下一篇:我想標記日期,但我不知道該怎么做
