有趣的findLast
将返回MDetail?
,因此var aa
的值可能为空。
有趣的remove
接受一个非null参数,但是为什么可以编译代码listofMDetail.remove(aa)
呢?谢谢!
还有,代码A可以正常运行!
代码A
private val listofMDetail:MutableList<MDetail> = myGson.fromJson<MutableList<MDetail>>(mJson)
fun deleteDetailByID(_id:Long){
var aa=listofMDetail.findLast { it._id == _id };
//listofMDetail.remove(null) //It doesn't work
listofMDetail.remove(aa) // It can be compiled
var bb: MDetail?=null
listofMDetail.remove(bb) // It can be compiled
}
源代码
public interface MutableList<E> : List<E>, MutableCollection<E> {
// Modification Operations
override fun add(element: E): Boolean
override fun remove(element: E): Boolean
...........
}
答案 0 :(得分:4)
在您的代码中,aa
和bb
均为MDetail?
类型,但是null
值本身不包含有关类型的信息,因此编译器无法推断类型对您来说,这是一个编译错误,但是如果将null
强制转换为MDetail?
,那么它也会被编译:
listofMDetail.remove(null as MDetail?)
然后的问题是,当您的remove
声明为listofMDetail
且MutableList<MDetail>
之后没有?
时,为什么MDetail
起作用。
这是因为remove
方法没有解析为public interface MutableList<E>
,而是MutableCollections.kt
的{{1}},下面是代码:
remove
在您的情况下,通用类型T为package kotlin.collections
/**
* Removes a single instance of the specified element from this
* collection, if it is present.
*
* Allows to overcome type-safety restriction of `remove` that requires to pass an element of type `E`.
*
* @return `true` if the element has been successfully removed; `false` if it was not present in the collection.
*/
@kotlin.internal.InlineOnly
public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection<out T>.remove(element: T): Boolean
= @Suppress("UNCHECKED_CAST") (this as MutableCollection<T>).remove(element)
,而MDetail?
为MDetail
,因此out T
将收到类型为remove
的参数,允许使用MDetail?
值。