alllex
05/06/2021, 9:10 AM@SinceKotlin("1.3")
@kotlin.internal.InlineOnly
public inline fun <C, R> C.ifEmpty(defaultValue: () -> R): R where C : CharSequence, C : R =
if (isEmpty()) defaultValue() else this
You can call it and it works great. However, if I simply copy this function into my code (with intention to later have a slightly modified version), it simply does not compile:
public inline fun <C, R> C.ifEmpty2(defaultValue: () -> R): R where C : CharSequence, C : R =
if (isEmpty()) defaultValue() else this
Produces error highlighting `CharSequence`:
Type parameter cannot have any other bounds if it's bounded by another type parameter
You can find the example here: https://pl.kotl.in/oGzbGuXah
Why is this the case? Why can’t I use the same features used in stdlib?Albert Chang
05/06/2021, 10:08 AM@kotlin.internal.InlineOnly
annotation is present. Source here.alllex
05/06/2021, 4:05 PMYoussef Shoaib [MOD]
05/07/2021, 7:31 PM@Suppress("BOUNDS_NOT_ALLOWED_IF_BOUNDED_BY_TYPE_PARAMETER")
Or for the "future-proof" solution:
@file:Suppress("INVISIBLE_MEMBER, INVISIBLE_REFERENCE")
@kotlin.internal.InlineOnly fun...
Or if you don't want any compiler error suppression, then create a file with the package kotlin.internal
and copy over the code for @InlineOnly
and then you'll be able to use it right away.