Hi Chris

This is a tradeoff, but one that in practice can be quite useful. inject[]
can fail with a runtime error if no binding is supplied, while
injectOptional always avoids a runtime error, since there is always a
default to fall back on. It's a bit like using .get or .getOrElse on an
option - the latter is preferred since it moves any remaining problems down
to compile time and won't fail at runtime. Subcut 3.0 (currently being
written) uses typeclasses for the default provider to avoid this runtime
problem as well, but that is not ready yet.

The other advantage to injectOptional is that someone reading the code in
the IDE can immediately see the "normal" implementation for that dependency
and click through to it from the IDE, rather than having to look up and
puzzle out the config. At testing time, the dependency can still be
intercepted and replaced. These are the reasons I use that form. Up to you
though, but the use of straight inject makes me feel a little unsafe right
now.

Dick


On Thu, Jul 4, 2013 at 12:18 AM, Christopher Hunt
<notifications@github.com>wrote:

> In app/controllers/Application.scala:
>
> > @@ -10,7 +10,8 @@ import com.escalatesoft.subcut.inject.{BindingModule, Injectable}
> >   */
> >  class Application(implicit val bindingModule: BindingModule) extends Controller with Injectable {
> >
> > -  val textGenerator = inject[TextGenerator]
> > +  // injectOptional provides safe interception of bindings, but falling back on a default implementation otherwise
> > +  val textGenerator = injectOptional [TextGenerator] getOrElse new WelcomeTextGenerator
> >
>
> Application now knows about WelcomeTextGenerator... there could be any
> number of implementations...
>
> —
> Reply to this email directly or view it on GitHub<https://github.com/huntc/play-subcut/pull/1/files#r5026179>
> .
>



-- 
Dick Wall
Email: dick@bldc.org    Gmail: dickwall@gmail.com     Skype: dickwall
Co-Host of the Java Posse: http://javaposse.com    Twitter: @dickwall