Atom Feed SITE FEED   ADD TO GOOGLE READER

Why no Multibindings for Lists or Arrays?

The Multibindings code I checked in last week has been well received. There's even a screencast introducing the new code.

But there are missing features. We're still figuring out exactly what's the right balance of functionality and simplicity. One particular omission is the lack of support for Lists and arrays.

This is intentional. The deal breaker with Lists and arrays is that they imply an explicit order of their elements. But Multibinder has no idea how to order the elements. It could try to keep elements of the same module in the same order they were bound, but all bets are off when the bindings come from multiple modules. And it would be very annoying if reordering your modules changed the behaviour of your program.

But if you really want a List...


Its not to hard to make a List<String> binding using the Set<String> binding that Multibinder gives you.
    bind(new TypeLiteral<List<String>>() {})
.to(new Provider<List<String>>() {
@Inject Provider<Set<String>> setProvider;

public List<String> get() {
/* if we wanted to, we could sort the list somehow */
return Collections.unmodifiableList(
new ArrayList<String>(setProvider.get());
}
});

Using this technique, you could also bind Map<K, V> if you've got a binding to Set<Map.Entry<K, V>>. We're thinking about adding built-in support for Maps, but this will work until we do.
How about turning Sets into Lists by setting an (optional) Comparator to the specific Multibinder.