Function Reference injection doesn't work unless provided type is specified explicitly
See original GitHub issueIf 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:
- Created 2 years ago
- Comments:6 (5 by maintainers)
Top 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 >
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
Added a note in the docs.
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.