-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kotlin.Lazy
support
#2353
Comments
Hi @sergeys-opera, IIUC, In order to reference it without
I'm also not a Kotlin expert, so please let me know if I missed something. |
@bcorso I get your point but I think that
is simpler than
I even wrote an extension method for
to write like this:
I'm quite satisfied with an extension method mentioned above but having native support would be even better. I also wonder if/why I'm the first one to request such feature. |
Ah yes, I just wanted to be clear that it wouldn't automatically allow you to write I do agree that it's probably worth supporting |
Fwiw, supporting operator fun <T> dagger.Lazy<T>.getValue(receiver: Any?, property: KProperty<*>): T = get() much more idiomatic than your |
@tbroyer this was the first thing I tried that but the code didn't compile ¯_(ツ)_/¯
There must be some difference between how compiler treats |
🤔 https://pl.kotl.in/nTI70NSnt Or an alternative with |
The Kotlin compiler does not special case 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 |
@tbroyer I realized that I put the property delegate declaration into a file located in a separate package and the mentioned
@JakeWharton you're probably right about that but why don't you need to import I agree that this task is probably not that important compared to other Kotlin improvements that can be made. Adding a property delegate extension method for |
Because it's a Kotlin class with an |
@tbroyer no, it's an extension according to this:
The actual reason why you don't need to import
|
FYI Inlining |
I think orienting this discussion around use of i.e. this code looks correct until dagger fails at runtime with a long trace about not providing a 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. |
Does dagger support lazy constructor injection?
Does it work as expected like we have lazy field injection for B object? |
Yes
Yes |
Is there any plan to add this? Just want to share my use case, in case someone is interested. I have the following module structure:
Now, the thing that I try to do is keeping the UI module away from dagger dependencies. I just want to have If I need to inject something lazily in the ui module, adding dagger.Lazy simply violates that and make the view dependent on Dagger. Hope this makes sense. |
@Clement-Jean you can use
|
Right, just more boilerplate I guess. Thank you for the solution though :) |
It would be nice to use
kotlin.Lazy
for lazily instantiated dependencies instead ofdagger.Lazy
in Kotlin code and omit som.get()
method calls.Then instead of:
it would be possible to write:
The text was updated successfully, but these errors were encountered: