Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify a generic type parameter in a Kotlin @Deprecated annotation?

Tags:

kotlin

I want to rename a Kotlin type, leaving a deprecated typealias to allow existing code to compile.

typealias TestContext<F> = ContextBuilder<F>

@Deprecated("TestContext is now ContextBuilder", 
    replaceWith = ReplaceWith("ContextBuilder<F>"))

leads to TextContext<Unit> being replaced with ContextBuilder<F>

@Deprecated("TestContext is now ContextBuilder", 
    replaceWith = ReplaceWith("ContextBuilder"))

leads to TextContext<Unit> being replaced with ContextBuilder

How can I write the replaceWith expression so that IntelliJ replaces TextContext<Unit> with ContextBuilder<Unit>?

like image 348
Duncan McGregor Avatar asked Jan 30 '19 20:01

Duncan McGregor


People also ask

How do I create a generic variable in Kotlin?

We can make generic variable using this kind of syntax: "val destinationActivity: Class<*>". Main part is "*".

How do I call generic method Kotlin?

Kotlin generic example When we call the generic method <T>printValue(list: ArrayList<T>) using printValue(stringList), the type T of method <T>printValue(list: ArrayList<T>)will be replaced by String type.

How do I find my generic type Kotlin?

There are no direct ways to do this in Kotlin. In order to check the generic type, we need to create an instance of the generic class<T> and then we can compare the same with our class.

What is the out keyword in Kotlin?

"Out" keyword is extensively used in Kotlin generics. Its signature looks like this − List<out T> When a type parameter T of a class C is declared out, then C can safely be a super type of C<Derived>. That means, a Number type List can contain double, integer type list.


1 Answers

This is possible now! The ReplaceWith intention saves generic type arguments from Kotlin plugin v1.3.40 on (see change log).

like image 114
Willi Mentzel Avatar answered Oct 16 '22 19:10

Willi Mentzel