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)
- Improve documentation to better understand how to use kotlin-inject HOT 4
- Conflicting overloads error when upgrading another library to use KSP 2.0.0 HOT 5
- Can't inject provided dependency into java class HOT 5
- There seem to be no way to define bindings in a separate file (like module for dagger) HOT 2
- Generated code tries to use property or variable that isn't there HOT 1
- Incremental compilation sometimes fails with exception HOT 2
- In some cases scope doesn't apply to provided dependency HOT 1
- Is nesting @Assisted possible? HOT 1
- Support qualifieres in multibinds map keys and set elements. HOT 2
- Resolving type arguments may throw an exception for star projections
- Annotation support for exposing a supertype to the graph HOT 6
- Implementation for scoped components HOT 7
- Critical obfuscation issue. HOT 8
- How to create ViewModel in Jetpack Compose? HOT 2
- How to inject WorkManager? HOT 3
- Checking for cyclic dependencies HOT 1
- KmpComponentCreate not generated for IosSimulatorArm64 HOT 5
- Different behavior between extension property and function when providing a scoped class using `@Qualifier` annotations
- The order of dependencies with `@Qualifier` annotation modifies the scope of the dependency.
- Build fails when declaring two `Lazy` dependencies.
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.