https://kotlinlang.org logo
#stdlib
Title
# stdlib
d

diego-gomez-olvera

02/21/2023, 5:21 PM
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

Adam S

02/21/2023, 5:22 PM
@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

diego-gomez-olvera

02/21/2023, 5:24 PM
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

ilya.gorbunov

02/21/2023, 6:33 PM
If you have a java function, you can annotate it with
kotlin.Deprecated
annotation and this deprecation should have effect only in Kotlin
d

diego-gomez-olvera

02/22/2023, 8:36 AM
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
8 Views