public inline fun <C, R> C.ifEmpty(defaultValue: () -> R): R where C : CharSequence, C : R =
if (isEmpty()) defaultValue() else this
But when copy&pasting that code it doesn’t even compile!
Type parameter cannot have any other bounds if it’s bounded by another type parameter
Is it safe to silence that error? 😮
Marc Knaup
08/26/2020, 11:04 AM
It seeeeeems to work
u
udalov
08/26/2020, 1:37 PM
IIRC this error is reported because generic signatures on JVM do not support type parameters having more than one non-interface bound (side note: I wonder why it’s reported on other platforms though), and the resulting generic signature would probably be incorrect, which can trip up client code in Java and other JVM languages.
For
ifEmpty
specifically, it seems OK to suppress it because the function is
inline
+
@InlineOnly
, so inaccessible from Java. In your case it might also be fine if you don’t have Java clients. If you do, you can prevent them from calling it by making it InlineOnly as well, however that leads to more error deprecations…
m
Marc Knaup
08/26/2020, 1:45 PM
Thank you for the explanation.
I’m absolutely fine with excluding other JVM clients 🙂
What do you mean by “more error deprecations”? That I have to