Giter Site home page Giter Site logo

Comments (14)

saket avatar saket commented on May 20, 2024

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.

evant avatar evant commented on May 20, 2024

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.

evant avatar evant commented on May 20, 2024

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.

evant avatar evant commented on May 20, 2024

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.

saket avatar saket commented on May 20, 2024

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.

swankjesse avatar swankjesse commented on May 20, 2024

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.

evant avatar evant commented on May 20, 2024

With the proposed impl, this would fail, there is no backtracking.

from kotlin-inject.

evant avatar evant commented on May 20, 2024

Added a test to validate this behavior. @swankjesse Mind elaborating on what the issue is with how guice does it?

from kotlin-inject.

swankjesse avatar swankjesse commented on May 20, 2024

Guice does backtracking and that's a mess. If you're not doing that you're good!

from kotlin-inject.

saket avatar saket commented on May 20, 2024

This doesn't seem to work with lambda parameters. Was that an intentional choice?

@Inject
class App(foo: () -> String = { "bar" })

from kotlin-inject.

evant avatar evant commented on May 20, 2024

What exactly do you mean by 'doesn't work'?

from kotlin-inject.

saket avatar saket commented on May 20, 2024

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.

evant avatar evant commented on May 20, 2024

Sounds like a bug, will take a look

from kotlin-inject.

evant avatar evant commented on May 20, 2024

Yep, found the issue. Pr up.

from kotlin-inject.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.