Is it possible to deprecate a function only for Ko...
# stdlib
d
Is it possible to deprecate a function only for Kotlin? Imagine that I have a function in Java
Copy code
public static boolean isEmpty(@Nullable CharSequence str) {
	return str == null || str.length() == 0;
}
And I would like to deprecate it for Kotlin users. I can change it to use an stdlib function
Copy code
@JvmStatic
@Deprecated("Use `isNullOrEmpty`", replaceWith = ReplaceWith("str.isNullOrEmpty()"))
fun isEmpty(str: CharSequence?): Boolean = str.isNullOrEmpty()
and it will warn Kotlin users and guide them towards the stdlib function, however it will also show as
@deprecated
for Java, where it is still needed (
isNullOrEmpty
is
InlineOnly
). Is it possible to show as deprecated only for Kotlin?
a
@JvmSynthetic
is the exact opposite of what you want... https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-synthetic/ Maybe you could do something clever with
@JvmName
...?
d
let me try...
ah, yes. I recall something inspired by OkHttp Kotlin conversion
Copy code
@JvmStatic
fun CharSequence?.isEmpty(): Boolean = isNullOrEmpty()

@JvmName("-deprecated_empty")
@Deprecated("Use `isNullOrEmpty`", replaceWith = ReplaceWith("str.isNullOrEmpty()"))
fun isEmpty(str: CharSequence?): Boolean = str.isNullOrEmpty()
thanks!
i
If you have a java function, you can annotate it with
kotlin.Deprecated
annotation and this deprecation should have effect only in Kotlin
d
ah, good to know!
I have used
Copy code
@Deprecated(message = "Use `isNullOrEmpty` instead",
		replaceWith = @ReplaceWith(expression = "str.isNullOrEmpty()", imports = ""))
public static boolean isEmpty(@Nullable CharSequence str) {
	return str == null || str.length() == 0;
}
However from the Kotlin code it appears deprecated but without the
ReplaceWith
suggestion