On Jun 7, 8:39 pm, Sam Berlin <[email protected]> wrote:
> It is necessary to maintain support for the current state, where two
> different implementations can co-exist as a singleton.  

I agree - I'm not suggesting an incompatible change.

> Consider:
>
>  bind(Server.class).annotatedWith(named("public")).to(ServerImpl.class).in(S 
> copes.SINGLETON);
>  bind(Server.class).annotatedWith(named("private")).to(ServerImpl.class).in( 
> Scopes.SINGLETON);

Actually, singleton-ness has nothing to do with what I am proposing.
I simply encountered the problem in the case of a thing which needed
to be a singleton.

I am simply suggesting that there should be a way to bind Server ->
AbstractServer -> ServerImpl -> ServerImplSubclass in a single call.
Whether it is a singleton or has any scope or is @Named is entirely
orthagonal.

//allow @Inject Server | AbstractServer | ServerImpl |
ServerImplSubclass to succeed w/o explicit bind() calls for each type
bindHierarchy(Server.class).annotatedWith(named("public")).to(ServerImpl.class);

This is just specifying an array of types to bind to the same
implementation class, while taking advantage of the fact that
inheritance dictates the contents of that array, so you can specify
the entire array simply by indicating the head and tail of it.  There
is no ambiguity since a hypothetical @Inject AbstractServer2 cannot be
an instance of { Server, AbstractServer, ServerImpl,
ServerImplSubclass }.

My apologies if my example suggested this had something to do with
singletons.

The irony of all this being, I generally believe in avoiding deep
inheritance hierarchies like the plague.  However, if you are writing
something for other people to extend, your audience is most likely
people who expect to use inheritance and will be baffled otherwise.

-Tim

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to