Youssef Shoaib [MOD]
01/28/2021, 3:42 PMifEmpty
extension function on String is defined like this in the Kotlin stdlib:
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?Andrew Neal
01/28/2021, 5:02 PM@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:
@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
CLOVIS
01/28/2021, 7:54 PM