Difference between Delegates.notNull and lateinit Kotlin

Kotlin

Kotlin Problem Overview


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)
    }

Kotlin Solutions


Solution 1 - Kotlin

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".

Solution 2 - Kotlin

  • 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSaiView Question on Stackoverflow
Solution 1 - KotlinJayson MinardView Answer on Stackoverflow
Solution 2 - KotlinTheTechWolfView Answer on Stackoverflow