I am very confused both looks and works pretty similar. Which one should I go for?
private var mMediaController by Delegates.notNull<MediaControllerCompat>()
or
lateinit private var mMediaController: MediaControllerCompat
Usage:
@Subscribe
fun connectToSession(token: MediaSessionCompat.Token) {
mMediaController = MediaControllerCompat(activity, token)
mMediaController.registerCallback(mMediaControllerCallback)
}
The last built-in delegate the Kotlin Standard Library offers is Delegates. notNull() . notNull() simply allows a property to be initialized at a later time. notNull() is similar to lateinit . In most cases, lateinit is preferred since notNull() creates an extra object for each property.
The notNull delegate notNull is the simplest of the four stdlib delegates. It works similar to lateinit in that it will throw an IllegalStateException if a variable is accessed before it is initialized. var age by notNull<Int>() fun main() = println(age) > Exception in thread "main" java. lang.
The two models are similar, and one predates the other. Delegates.notNull()
(api reference) is based on delegated properties and was the original, and later came lateinit
(Late Initialized Properties). Neither cover all possible use cases and neither should be used unless you can control the lifecycle of the class and know for sure that they will be initialized before being used.
If the backing field might be set directly, or your library cannot work with a delegate then you should use lateinit
and typically it is the default for most people when using with dependency injection. From the docs:
Normally, properties declared as having a non-null type must be initialized in the constructor. However, fairly often this is not convenient. For example, properties can be initialized through dependency injection, or in the setup method of a unit test. In this case, you cannot supply a non-null initializer in the constructor, but you still want to avoid null checks when referencing the property inside the body of a class.
If the type you are using is not supported by lateinit
(does not support primitive types) you are then forced to use the delegate.
The (lateinit) modifier can only be used on var properties declared inside the body of a class (not in the primary constructor), and only when the property does not have a custom getter or setter. The type of the property must be non-null, and it must not be a primitive type.
You might also want to read the discussion topic "Improving lateinit".
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With