FYI - I'm combinding constant and instance bindings in the SPI

2 views
Skip to first unread message

limpb...@gmail.com

unread,
Jul 31, 2008, 12:20:24 PM7/31/08
to google-guice-dev
I'm working on the SPI and I'm planning on removing
the ConstantBinding altogether, combining it with our
InstanceBinding. Mostly for my own sake, I'll enumerate
the differences between these two types to illustrate
why this change is okay...

1. How they are bound
bindConstant().annotatedWith(...).to("hello");
bind(String.class).annotatedWith(...).toInstance("hello");
The syntax is different. We'll support both, but I don't
think the Elements API will distinguish between which
syntax was used.

2. How they are injected
Instance bindings are injected at bind time, constant
bindings are not. For most of the supported types, injecting
a constant binding doesn't really hurt - String, Integer, Class
etc. don't have anything to inject, so at worst this could
be a slight regression in performance as we look for the
injection points in String.class.

The only exception is enums. The following do not have
identical behaviour if TAILS has any injected fields or
methods:
bindConstant().annotatedWith(...).to(CoinSide.TAILS);
bind(CoinSide.class).annotatedWith(...).toInstance(CoinSide.TAILS);
As far as I'm concerned, the fact that one syntax injects and
the other doesn't is already arbitrary (and potentially surprising).
Making these behave the same way is A Good Thing.

3. How they are used internally
Internally to Guice, constant and instance bindings are
exactly the same. The behaviour is indistinguishable.

4. The SPI
This is the most controversial part. Currently the SPI
exposes constant bindings and instance bindings as
distinct things. In practice I've found this to be more
of a source of pain than pleasure. For example, any
code that reflectively examines bindings needs to handle
both the constant and regular binding cases. Often the
constant case is accidentally overlooked.

In the Modules.overrideModules() code, we need to handle
both constant and regular bindings, even though both code
paths are the same.
http://code.google.com/p/google-guice/source/browse/trunk/src/com/google/inject/Guice.java


I'm going to make this change. The consequence is that
in the SPI, we won't be able to distinguish between a
bindConstant( ) and the corresponding bind().toInstance().

Please let me know if there's something I've overlooked!

Cheers,
Jesse
Reply all
Reply to author
Forward
0 new messages