Status: New
Owner: ----

New issue 657 by [email protected]: Type-safe means to build parameterized TypeLiterals from existing TypeLiterals/class tokens
http://code.google.com/p/google-guice/issues/detail?id=657

Various framework code based on Guice needs to bind a parameterized type where some of the type arguments contain in-scope type variables, which obviously must be reified by existing type tokens of some kind. A contrived example:

abstract class FancyModule extends AbstractModule {
  <T> AnnotatedBindingBuilder<List<T>> bindListOf(Class<T> classT) {
    TypeLiteral<List<T>> tl_LT = /* ??? */;
    return bind(tl_LT);
  }
}

One would like to just do:

    TypeLiteral<List<T>> tl_LT = new TypeLiteral<List<T>>() {};

but there is no way to pass in classT, so Guice will inevitably raise the error "List<T> cannot be used as a key; It is not fully specified" when it discovers the information is missing. The following works, but is clunky and incurs an unchecked warning:

    TypeLiteral<List<T>> tl_LT = (TypeLiteral<List<T>>) \
      TypeLiteral.get(Types.newParameterizedType(List.class, classT));

I propose to introduce new variants of the TypeLiteral class that take additional type parameters and corresponding TypeLiteral tokens, like this:

abstract class TypeLiteralV1<T, A> extends TypeLiteral<T> {
    protected TypeLiteralV1(TypeLiteral<A> tokA) { ... }
}

    TypeLiteral<T> tl_T = TypeLiteral.get(classT);
    TypeLiteral<List<T>> tl_LT = new TypeLiteralV1<List<T>, T>(tlT) {};

The implementation then reads the anonymous subclass's extends declaration "TypeLiteralV1<List<T>, T>" to learn that T is reified by tl_T, and thus uses tl_T to construct a reification of List of whatever T actually is.

I have sample code for this construction from a Google internal project, and the code has just been approved for open-source release. I will be submitting a patch based on this code to Guice.

--
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" 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-dev?hl=en.

Reply via email to