saw this in Strings.kt for `String.trim()` and im ...
# announcements
t
saw this in Strings.kt for
String.trim()
and im just curious, why the cast to CharSequence?
Copy code
/**
 * Returns a string having leading and trailing whitespace removed.
 */
@kotlin.internal.InlineOnly
public inline fun String.trim(): String = (this as CharSequence).trim().toString()
k
Otherwise it would be recursive.
this.trim()
would call
String.trim()
instead of
CharSequence.trim()
👆 2
t
oh right 🙂 neat, thanks!
s
Also, when you see an 'odd' thing like this (seemingly unnecessary up-cast), extension functions are statically bound
👍 2
m
I guess this is an important consideration when deciding whether to declare an extension function vs. instance function.