Justin Tullgren
11/03/2021, 7:10 PMDeprecated
annotation ReplaceWith
when the argument is a receiver function type?
@Deprecated
fun factory(initializer: Builder.() -> Unit): Type = Builder().apply(initializer).build()
Justin Tullgren
11/03/2021, 7:12 PMDeprecated
Joffrey
11/04/2021, 10:18 AMJustin Tullgren
11/05/2021, 1:49 AMJustin Tullgren
11/05/2021, 2:08 AMJustin Tullgren
11/05/2021, 2:09 AMpublic fun URI.modified(): URI = URI(this.toString() + "/example")
Justin Tullgren
11/05/2021, 2:10 AMfun main() {
// stays the same but the import should change
val modified = URI("<example://ex>").modified()
}
Justin Tullgren
11/05/2021, 4:42 PMJoffrey
11/05/2021, 4:53 PMthis
in the replacement string:
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:
@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 packageJustin Tullgren
11/05/2021, 8:30 PMJustin Tullgren
11/08/2021, 4:11 PMJoffrey
11/08/2021, 4:44 PMReplaceWith
in this case does work, but it transforms the lambda into an anonymous function. Not very beautiful, but at least the code is correct.