macwire icon indicating copy to clipboard operation
macwire copied to clipboard

Transitive dependencies

Open dsilvasc opened this issue 8 years ago • 4 comments

If class A depends on B, which depends on and I have a C, can I instantiate an A without also wiring up a B? In other words, would it be possible for this:

val c: C = ...

val a = wire[A]

to generate this?

val c: C = ...

val _macwire_b = new B(c)
val a = new A(b)

Similarly, if I don't have a C in scope but C has a no-args constructor, can macwire instantiate that as well? That is, this:

val a = wire[A]

would become:

val a = new A(new B(new C))

dsilvasc avatar Dec 06 '17 15:12 dsilvasc

I asked a similar question on the scala gitter channel today about how implicits handle this: https://gitter.im/scala/scala?at=5a280d2e3ae2aa6b3f93a3ab

dsilvasc avatar Dec 06 '17 16:12 dsilvasc

That's not currently supported, but could be a very useful feature! Maybe it could be called e.g. wireDeep[]?

As for achieving the same of implicits, I don't think it's possible to achieve that result without additional code.

adamw avatar Dec 06 '17 19:12 adamw

One problem here would be loss of control over instance scope. That is, each time there's a deeply-wired dependency on B, a new instance of B would be created, instead of re-using an existing one. That might not be an issue, but good to keep in mind.

adamw avatar Dec 07 '17 08:12 adamw

I think that's a fair condition. Java DI frameworks like Guice work the same way, and JSR-330 provides the javax.inject.Singleton annotation to denote classes that should be instantiated only once by libraries or frameworks.

dsilvasc avatar Dec 07 '17 08:12 dsilvasc