Hi,
I am fairly new to Guice. An important use-case for me is interface-
based programming. I am trying to find out whether it is possible to
capture in a single binding statement all possible instantiations of a
parametric type.
The naive approach of using the raw type for binding does not work.
Consider the following example:
interface I<T> {...}
class A<T> implements I<T> {...}
bind(I.class).to(A.class);
@Inject
foo(I<String> x) { ... } // run-time error
@Inject
bar(I<Integer> x) { ... } // run-time error
It seems that injection needs the fully instantiated type to be
specified at both injection point and binding point:
bind(new TypeLiteral<I<String>>(){}).to(new TypeLiteral<A<String>>()
{});
bind(new TypeLiteral<I<Integer>>(){}).to(new TypeLiteral<A<Integer>>()
{});
...
However, this is tedious, and I am surprised that there seems to be no
support for what one may call 'generic bindings': where binding
specifications bind a raw interface to a raw implementation, but
injection happens with respect to instantiated parametric types.
Is there any particular reason not to support such 'generic bindings'?
Or am I just missing a trick?
Thanks,
-David.
--
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.