Difference between Delegates.notNull and lateinit Kotlin

后端 未结 2 1414
渐次进展
渐次进展 2020-12-09 14:47

I am very confused both looks and works pretty similar. Which one should I go for?

private var mMediaController by Delegates.notNull

        
2条回答
  •  旧时难觅i
    2020-12-09 15:34

    • notNull creates an extra object for each property.

    • The object is small, but if you have lots of properties, it can be significant for you.

    • You can't use the notNull delegate with external injection tools that injects directly to Java fields;

    • You can't create a lateinit property of the primitive type (Int, Long, etc).

    • lateinit is cheaper, but you can use only the delegate when your property has a primitive type.

    Source: https://discuss.kotlinlang.org/t/notnull-delegate-vs-lateinit/1923/2

提交回复
热议问题