Reputation: 8353
I know that the best way to deal with dependency injection in Scala is using tools that were built specifically for the language, but I working on a project that must integrate some Scala and Java code.
Then, I am using Google Guice, that implements specification JSR-330. Fortunatly, I found no problem during integration of Guice and Scala. I am using constructor injection, because I have to deal with immutability.
My question is, why in Scala we have to use the notation @Inject()
in front of the constructor parameter? Why the ()
paranthesis? It follows an example:
class MyClass @Inject() (val another: AnotherClass) {
// Body of the class
}
Upvotes: 7
Views: 2167
Reputation: 170745
Otherwise, how could you tell if (val another: AnotherClass)
is constructor parameter list or arguments to @Inject
?
Upvotes: 4
Reputation: 6523
It is all about the syntax of annotating a constructor. Scala requires a constructor annotation to have one (and exactly one) parameter list (even if it is empty)
class Bar @Fooable() ( val i : Int) {
}
What would the i
parameter belong to below: The annotation or the Bar
class?
class Bar @Fooable( val i : Int) {
}
Upvotes: 3