Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Deprecated ReplaceWith work for Kotlin in intellij?

Tags:

I wrote this code:

@Deprecated("Old stuff", ReplaceWith("test2"))
fun test1(i: Int) {
    println("old Int = $i")
}

fun test2(i: Int) {
    println("new Int = $i")
}

fun main(args: Array<String>) {
    test1(3)
}

and for some reason when I press Alt+Enter and click "Replace with test2", the method test1 disappears and doesn't get replaced, what am I doing wrong?

Edit:

It does work for classes though:

@Deprecated("Old stuff", ReplaceWith("Test2"))
class Test1
class Test2

fun main(args: Array<String>) {
    val a = Test1()
}
like image 843
Coder-Man Avatar asked Sep 06 '18 11:09

Coder-Man


People also ask

How do you use deprecated method Kotlin?

To help removing deprecated API gradually, the property level could be used. Usually a gradual phase-out goes through the "warning", then "error", then "hidden" or "removed" stages: First and by default, DeprecationLevel. WARNING is used to notify API consumers, but not to break their compilation or runtime usages.

Is deprecated deprecated in Java Kotlin?

Kotlin Android Extensions is deprecated, which means that using Kotlin synthetics for view binding is no longer supported.


1 Answers

You need to tell how it needs to be replaced exactly... While I do not know why it was just completely deleted, I will show you what I mean instead:

If you would use the following instead:

@Deprecated("Old stuff", ReplaceWith("test2(i)"))

it will replace your test1(5) call to test2(5) correctly.

Note also that sometimes you may want to add the package name also if it isn't that clear which replacement should take place, e.g.:

@Deprecated("Old stuff", ReplaceWith("org.example.test2(i)"))
// or just use:
@Deprecated("Old stuff", ReplaceWith("test2(i)", /* here come the imports */ "org.example.test2"))

You can also use static values in the replacement in case that is what you need.

like image 135
Roland Avatar answered Oct 21 '22 03:10

Roland