question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`kotlin.Lazy` support

See original GitHub issue

It would be nice to use kotlin.Lazy for lazily instantiated dependencies instead of dagger.Lazy in Kotlin code and omit som .get() method calls.

Then instead of:

class AccountManager @Inject constructor(
    service: dagger.Lazy<AccountService>
) {
  service.get().getAccount()
}

it would be possible to write:

class AccountManager @Inject constructor(
    service: kotlin.Lazy<AccountService>
) {
  service.getAccount()
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17

github_iconTop GitHub Comments

7reactions
ZacSweerscommented, Feb 28, 2021

I think orienting this discussion around use of dagger.Lazy as delegate buried the lede a bit. It would be nice, but the biggest value prop IMO would be avoiding the constant papercut that is Kotlin’s implicit import of its own Lazy.

i.e. this code looks correct until dagger fails at runtime with a long trace about not providing a Lazy, something I’ve seen multiple colleagues (and myself) get burned by.

class TestClass @Inject constructor(val dependency: Lazy<Dependency>)

this looks right but fails to compile, and kotlin users reflexively will try to write it this way. This costs cycles and cognitive overhead

+import dagger.Lazy
+
class TestClass @Inject constructor(val dependency: Lazy<Dependency>)

I think the combination of this and the free delegate support make this a pretty compelling addition. If not, I’d want to probably sent a PR to add a lint check for this case instead since it comes up regularly.

2reactions
JakeWhartoncommented, Feb 4, 2021

The Kotlin compiler does not special case kotlin.Lazy. It’s entirely a library feature that neither the compiler nor language are aware of.

In general I don’t see much advantage to adding support for Kotlin’s lazy in Dagger, but there’s also very little disadvantage I suppose. Dagger already has too many things that have more than one way of doing it. What’s one more.

I would rather see time spent on parsing Kotlin metadata to avoid generic variance mismatches that require @JvmSuppressWildcard or wiring up @Provide suspend fun and injectable Deferred<T> if we’re going to focus on Kotlin-specific features of Dagger.

Read more comments on GitHub >

github_iconTop Results From Across the Web

lazy - Kotlin Programming Language
Creates a new instance of the Lazy that uses the specified initialization function initializer and the default thread-safety mode LazyThreadSafetyMode.
Read more >
Lazy Initialization in Kotlin - Baeldung
See how you can achieve lazy initialization in Kotlin. ... Luckily, the Kotlin language has built-in support for lazy initialization.
Read more >
Initializing lazy and lateinit variables in Kotlin - LogRocket Blog
This article will explain how the lateinit modifier and lazy delegation can take care of unused or unnecessary early initializations.
Read more >
Kotlin Lazy — What are they? How to use them? - Medium
Why do you want to initialize your database manager class before you do any DB operations in your application? In mobile applications, the...
Read more >
Kotlin by Lazy and Lateinit - eduCBA
In kotlin, we have multiple features that can help us delay that object initialization as per requirement. Lazy initialization is the object creation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found