为什么我可以将空参数传递给在Kotlin中要求非空的乐趣?

时间:2018-07-19 00:25:37

标签: android kotlin

有趣的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

   ...........
}

1 个答案:

答案 0 :(得分:4)

在您的代码中,aabb均为MDetail?类型,但是null值本身不包含有关类型的信息,因此编译器无法推断类型对您来说,这是一个编译错误,但是如果将null强制转换为MDetail?,那么它也会被编译:

listofMDetail.remove(null as MDetail?)

然后的问题是,当您的remove声明为listofMDetailMutableList<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?值。