The `ifEmpty` extension function on String is defi...
# announcements
y
The
ifEmpty
extension function on String is defined like this in the Kotlin stdlib:
Copy code
public inline fun <C, R> C.ifEmpty(defaultValue: () -> R): R where C : CharSequence, C : R =
    if (isEmpty()) defaultValue() else this
However, if you try to define exactly the same function in your own code you get this error:
Type parameter cannot have any other bounds if it's bounded by another type parameter
So how is the standard library circumventing this error? I even checked and there aren't any
@Suppress
annotations in the file at all regarding this, so how in the world did they stop that error?
👍 2
a
The stdlib suppresses this by using
@kotlin.internal.InlineOnly
. https://github.com/JetBrains/kotlin/blob/871912f257dc37ed6640c302b0dc4d2a5306ce6f/[…]rontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt You can suppress the warning yourself too:
Copy code
@Suppress("BOUNDS_NOT_ALLOWED_IF_BOUNDED_BY_TYPE_PARAMETER")
public inline fun <C, R> C.ifEmpty(defaultValue: () -> R): R where C : CharSequence, C : R =
    if (isEmpty()) defaultValue() else this
c
Is it really safe? It sounded more like an error than a warning to me (not the original person)