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.

Allow to enforce Lazy<T>

See original GitHub issue

The problem:

I can’t provide Lazy<T> like this:

    @Provides
    public Lazy<DrawerLayout> provideDrawer

Error:(17, 10) error: android.support.v4.widget.DrawerLayout cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. xx.DocumentListFragment.drawer [injected field of type: dagger.Lazy<android.support.v4.widget.DrawerLayout> drawer]

Because of that some users of T may try to @Inject T dependency instead of using Lazy. Dagger have no idea that the dependency does not exist at the moment of injection so it may inject null or just crash inside of a module.

Solution: allow to provide Lazy<T> directly.

A temporary workaround I’m using (simplified):

public interface Delayed<T> {
    T get();
}

@Provides
Delayed<DrawerLayout> provideDrawerLayout() {...}

@Inject Delayed<DrawerLayout> drawer;

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
bcorsocommented, Nov 16, 2021

Yes. In general, anything that you can inject in a field you can also inject into a constructor.

0reactions
asthagarg2428commented, Nov 16, 2021

Yes, see https://dagger.dev/dev-guide/#lazy-injections

No, I mean does it support lazy injection in constructor also?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lazy Initialization in Spring Boot 2.2 - Baeldung
Here, we use the setLazyInitialization method to configure our application to be initialized lazily. One important note to remember is that ...
Read more >
Lazy Initialization - .NET Framework - Microsoft Learn
Explore lazy initialization in .NET, a performance improvement that means an object creation is deferred until the object is first used.
Read more >
What is Lazy Loading | Lazy vs. Eager Loading - Imperva
Lazy loading is an optimization technique that delays the loading of objects, allowing for a quicker load time. Find out how it can...
Read more >
5 ways to initialize lazy associations and when to use them
5 ways to initialize lazy associations and when to use them ; 1 1. Call a method on the mapped relation ; 2...
Read more >
Module Lazy - OCaml library
Module Lazy ; force x forces the suspension x and returns its result. If x has already been forced, Lazy.force x returns the...
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