On Saturday, April 21, 2012 3:52:48 AM UTC+2, Mirko Raner wrote: > > On Apr 19, 11:04 am, Mirko Raner <[email protected]> wrote: > > The closest I can come expressing this in Guice is > > > > binder.bind(new TypeLiteral<Map<?,?>>(){}).to(new > > TypeLiteral<HashMap<?,?>>(){}); > > > > but that doesn't seem to work. > > Sorry, another clarification -- apparently I'm really good at > confusing everyone ;-) : > > The above binding of course DOES work for injections like @Inject > Map<?,?> map (with a wildcarded type), which makes sense. In line with > Java's type system this binding does not match any other injected > types (like Map<String, String>). > What I would really like to bind is Map<K,V> (with K and V being > unbound type parameters), but there is really no way to express that. > I tried binding raw types (i.e., TypeLiteral<Map> to > TypeLiteral<HashMap>, but (as expected) that binding only works for > raw types at the injection point. > I guess as part of solving this one would have to introduce special > "any" or "unbound" classes to be used for specifying the bindings: for > example, TypeLiteral<Map<Any,Any>>, or TypeLiteral<List<Unbound>>, or > maybe even something like TypeLiteral<Map<_,_>> or TypeLiteral<Map<$, > $>>. > > I think at this point we all agree that Guice does not support this > use case. The follow-up questions, I guess, are: can it be implemented > and is it worth it? >
Looking at the docs [1], it seems like you could do it using a TypeListener [2]. I mean, if one can do AssistedInject and Multibinding, there's no reason you couldn't do "generic binding". I have absolutely no idea how you'd implement it though. [1] http://code.google.com/p/google-guice/wiki/CustomInjections [2] http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/spi/TypeListener.html -- You received this message because you are subscribed to the Google Groups "google-guice" group. To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/Dr4WRjqTzooJ. 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.
