ReplaceWith nothing


#1

I have chained some methods and one of those methods should be removed.
Is it possible to remove a funtion call?

class ExitCollection{
    val map =mutableMap<String, Room?>()
    fun filterNotNull() = map.filterValuesNotNull()
    fun forEach(...) = also(...)
}
fun main(varargs args: String){
    ExitCollection().filterNotNull().forEach{...}
    //should become
    ExitCollection().forEach{...}
}

#2

Use the following syntax:

class ExitCollection : MutableMap<String, Any?> by mutableMapOf() {
    fun filterNotNull() = filterValuesNotNull()
    fun forEach(...) = also(...)
}

fun main(varargs args: String){
    ExitCollection().filterNotNull().forEach{...}
    //should become
    ExitCollection().forEach{...}
}

It is called delegation. It helps implementing an interface using a function that returns an implementation of that interface.
In your case the interface is MutableMap<String, Room>, and the function is mutableMapOf from the standard library.


#3

oops my bad :blush:

My real question is if the ReplaceWith() of @Deprecated() has a feature to add a quickfix for removing a complete feature:

@Deprecated("filterNotNull is default and will be removed", ReplaceWith(""))

But this is not working, because the defaultvalue of Replacewith is ReplaceWith("").
So my question is: Is there another way for a quickfix to change

ExitCollection().filterNotNull().forEach{...}

into

ExitCollection().forEach{...}

#4

If filterNotNull just returns this, try ReplaceWith("this")