https://kotlinlang.org logo
#getting-started
Title
# getting-started
j

Justin Tullgren

11/03/2021, 7:10 PM
can anyone point me to how to use the
Deprecated
annotation
ReplaceWith
when the argument is a receiver function type?
Copy code
@Deprecated
fun factory(initializer: Builder.() -> Unit): Type = Builder().apply(initializer).build()
it keeps bringing in the deprecated builder even when i have added the imports argument to
Deprecated
j

Joffrey

11/04/2021, 10:18 AM
Could you please show an example of what you want the users to use instead of the function?
j

Justin Tullgren

11/05/2021, 1:49 AM
I really just want to do a package move, so same name, args, return, etc
same thing with extension functions too, i just want them to move to a different package
Copy code
public fun URI.modified(): URI = URI(this.toString() + "/example")
Copy code
fun main() {
// stays the same but the import should change
val modified = URI("<example://ex>").modified()
}
@Joffrey
j

Joffrey

11/05/2021, 4:53 PM
Oh sorry I missed your messages, for the extension function you can use
this
in the replacement string:
Copy code
package com.example.somepackage

@Deprecated(
    message = "",
    replaceWith = ReplaceWith("this.modified()", imports = ["com.example.otherpackage.modified"]),
)
fun URI.modified(): URI = URI(this.toString() + "/example")
And for the initial function you can use:
Copy code
@Deprecated(
    message = "",
    replaceWith = ReplaceWith("factory(initializer)", imports = ["com.example.otherpackage.factory"]),
)
fun factory(initializer: Builder.() -> Unit): MyType = Builder().apply(initializer).build()
All of this is of course assuming the same function exists in another package
j

Justin Tullgren

11/05/2021, 8:30 PM
awesome thank you! i'll try on monday and let you know how it goes!
👍 1
this doesn't work if anyone checks this thread FYI
🤔 1
j

Joffrey

11/08/2021, 4:44 PM
So FYI the
ReplaceWith
in this case does work, but it transforms the lambda into an anonymous function. Not very beautiful, but at least the code is correct.
4 Views