I think it's a relatively rare use case, which is why it's not supported as
an inferable JIT binding.
Also, I might want:
@Provide List<String> provideListOfStrings() {
return new ArrayList<String>();
}
@Provide List<Integer> provideListOfIntegers() {
return new LinkedList<Integer>();
}
Which implies that having Guice do such inference is not a good thing.
For your case, you could do this programmatically if you knew all the types
of T you wanted to inject using Types.newParameterizedType() class and
friends, with something like an array of Class[].
Fred
On Wed, Apr 20, 2011 at 11:38 AM, dl <[email protected]> wrote:
> 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.
>
>
--
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.