Comments (14)
I also noticed that the error message produced by kotlin-inject
doesn't use the correct type. For the above example, it would print this error message:
e: [ksp] Cannot find an @Inject constructor or provider for: String
from kotlin-inject.
Can look into it. My biggest concern is how confusing it is to know which params get injected and which ones don't. You can use a secondary constructor for now.
class Foo(val generator: () -> String) {
@Inject constructor() : this({ "bar" })
}
from kotlin-inject.
Note, that message is correct, if you have a @Provides
of type String you can also inject it as () -> String
or Lazy<String>
.
from kotlin-inject.
Put up a pr to make params with default values optional. For example:
@Inject class Foo(bar: Bar = Bar("default"))
@Component abstract class WithDefaultComponent {
abstract val foo: Foo // returns Foo(Bar("default"))
}
@Component abstract class WithoutDefaultComponent {
abstract val foo: Foo // returns Foo(Bar("override"))
@Provides fun bar(): Bar = Bar("override")
}
This actually works similarly to dagger's optional binding.
from kotlin-inject.
Awesome!
Note, that message is correct, if you have a @provides of type String you can also inject it as () -> String or Lazy.
Gotcha.
from kotlin-inject.
Optional injection has really ugly consequences when paired with constructor injection.
class ConnectivityService @Inject constructor(
val client: OkHttpClient,
val metrics: Metrics = Metrics(NoOpLogger)
)
class Metrics @Inject constructor(
val logger: Logger
)
interface Logger
What happens when I ask for a ConnectivityService if Logger is not bound? Does it fail because it can't satisfy Metrics’ constructor? Or does it succeed by using the default Metrics instance?
Guice’s implementation of optional injection does backtracking here and the consequences are completely awful, that design was a big mistake.
from kotlin-inject.
With the proposed impl, this would fail, there is no backtracking.
from kotlin-inject.
Added a test to validate this behavior. @swankjesse Mind elaborating on what the issue is with how guice does it?
from kotlin-inject.
Guice does backtracking and that's a mess. If you're not doing that you're good!
from kotlin-inject.
This doesn't seem to work with lambda parameters. Was that an intentional choice?
@Inject
class App(foo: () -> String = { "bar" })
from kotlin-inject.
What exactly do you mean by 'doesn't work'?
from kotlin-inject.
kotlin-inject
throws the same error:
e: [ksp] Cannot find an @Inject constructor or provider for: String
I can send a reproducible sample if you want, but this can be easily reproduced by converting the type of any parameter with a lambda:
@Inject
class App(
- foo: String = "bar"
+ foo: () -> String = { "bar" }
)
from kotlin-inject.
Sounds like a bug, will take a look
from kotlin-inject.
Yep, found the issue. Pr up.
from kotlin-inject.
Related Issues (20)
- Unclear error message for scoped provider HOT 2
- Providing a function instead of property seems to not always work HOT 1
- Ability to bind Implementaion with Interface HOT 1
- Calling scoped suspending provider should create singleton instance HOT 6
- Custom component and scope support like in Dagger 2
- Generated code is not accessible from commonMain HOT 5
- Search for a covariant type when checking if a component's abstract property has been implemented
- Cannot scope child components dependencies with parent component's scope using Provides HOT 6
- can't build the project HOT 1
- Error while compiling project HOT 5
- Feature request - support deferring to superclass component properties HOT 2
- Typealiases lead to KSP StackOverflowException
- Scope annotations aren't picked up when scanning superclasses
- Expect Actual Ancestor Components and Common Descendant Components HOT 5
- Kotlin Delegation Support? HOT 4
- Interface Bindings HOT 3
- Support @IntoSet for superclass HOT 1
- Names clash for properties in component with multiple super types HOT 2
- Component with an Int literal property name is used as an Int value
- Implemented function still considered abstract HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlin-inject.