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.

Function Reference injection doesn't work unless provided type is specified explicitly

See original GitHub issue

If I want to inject a function reference into a class, the build fails unless I explicitly specify the provided type for the function reference in the component. Without the return type, it defaults to the behaviour of trying to generate a provider and in the following example complains that Any can’t be provided.

i.e.
this does not work:

@Inject
class MyClass(private val myProvider: suspend () -> Any)

suspend fun theProvider(): Any { /* ... */ }

@Component
abstract class AppComponent {
  @Provides
  protected fun provideTheProvider() = ::theProvider
}

but this does:

@Inject
class MyClass(private val myProvider: suspend () -> Any)

suspend fun theProvider(): Any { /* ... */ }

@Component
abstract class AppComponent {
  @Provides
  protected fun provideTheProvider(): suspend () -> Any = ::theProvider
}

I’m not sure if this is intended behaviour or not, but if it is then it might be worth documenting.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
evantcommented, May 11, 2021

Added a note in the docs.

1reaction
evantcommented, Apr 24, 2021

Note: this isn’t the first issue I’ve run into with implicit @Provides return types. I think I’m going to add a recommendation that you always explicitly declare the @Provides return type. It seems like often-times it’s not the type you expect it to be.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use dependency injection in .NET Azure Functions
The dependency injection container only holds explicitly registered types. The only services available as injectable types are what are setup in ...
Read more >
Dependency injection and programmatic lookup
First, the container calls the bean constructor (the default constructor or the one annotated @Inject ), to obtain an instance of the bean....
Read more >
Contexts and Dependency Injection - Quarkus
Simplified Constructor Injection. In CDI, a normal scoped bean must always declare a no-args constructor (this constructor is normally generated by the ...
Read more >
What is dependency injection? - Stack Overflow
Dependency injection is basically providing the objects that an object ... When using dependency injection, objects are given their dependencies at run time ......
Read more >
Top 5 TypeScript dependency injection containers
Explore how to implicitly automate dependency management in TypeScript using a dependency injection container.
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