`kotlin.Lazy` support
See original GitHub issueIt 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:
- Created 3 years ago
- Comments:17
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 ownLazy
.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.this looks right but fails to compile, and kotlin users reflexively will try to write it this way. This costs cycles and cognitive overhead
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.
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 injectableDeferred<T>
if we’re going to focus on Kotlin-specific features of Dagger.