I'm looking at the scaly code example from play-mailer: https://github.com/playframework/play-mailer
It goes basically like this:
class MyComponent @Inject() (mailerClient: MailerClient) {
...
}
simple enough and it compiles without compliant
Then I try to "call" it however and there doesn't appear to be a way to satisfy the compiler OR get a working instance of mailerClient.
object AnObject {
val mailer = new MyComponent
def sendEmail = mailer.doStuff
}
[info] Compiling 1 Scala source to ...
[error] /SomeOne/SomePath/SomeFile.scala:30: not enough arguments for constructor MyComponent: (mailerClient: play.api.libs.mailer.MailerClient) MyComponent.
[error] Unspecified value parameter mailerClient.
[error] val mailer = new MyComponent
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
I though I might have gotten close thanks to this:
How does @Inject in Scala work
Which indicated that the following syntax might work by removing the @Inject
from the constructor and placing it on a field.
@Inject var mailerClient: MailerClient = null
However the moment we try to run anything that needs that reference we still get null.
I'm reading everything I can find on @Inject
( [warning] [rant] I'm NOT a fan of compiler magic like this for this exact reason -- voodoo magic is wonderful until it stops working then no one seems to have any idea of how to fix it. [/rant] [/warning] )
but what I really want to know is how to use it properly, safely and effectively.