在单个语句Kotlin中列出clear和addAll

Sai*_*Sai 8 kotlin

在kotlin中是否有任何方法将以下两行替换为一行.我知道我可以创建一个扩展函数,但我很想知道它是否已经存在于kotlin中.有点像listOfChecklist.clearAndAddAll().

listOfChecklist.clear()
listOfChecklist.addAll(newList)
Run Code Online (Sandbox Code Playgroud)

这就是我现在手动使用扩展功能.但我希望有更好的解决方案.

fun <E> MutableCollection<E>.clearAndAddAll(replace: MutableSet<E>) {
    clear()
    addAll(replace)
}
Run Code Online (Sandbox Code Playgroud)

s1m*_*nw1 7

这样做是不可能的.解决案例的惯用方法是使用范围函数with.我不认为这里有很大的优势.

val list = mutableListOf<String>()

with(list){
    clear()
    addAll(arrayListOf())
}
Run Code Online (Sandbox Code Playgroud)