Signature of Collections2#transform

115 views
Skip to first unread message

Olivier Michallat

unread,
Aug 18, 2010, 6:02:41 PM8/18/10
to guava-discuss
Hi all,

I've noticed a slight inconsistency between Collections2#transform and
Lists#transform: the first uses a "Function<? super F, T>", while the
latter uses a "Function<? super F, ? extends T>". Did I miss
something, or wouldn't it be better to have a consistent signature?

Allowing "? extends T" is more general, although it requires explicit
type parameters when the function returns a subtype:

Function<String, Integer> f = ...
List<String> input = ...
List<Number> output = Lists.<String, Number>transform(input, f);

Olivier

Kevin Bourrillion

unread,
Aug 18, 2010, 6:53:12 PM8/18/10
to guava-...@googlegroups.com
The wildcard on Lists.transform() was a mistake.  It serves no useful purpose; all it accomplishes is that it allows the resulting list to be typed as a List<SuperFoo> instead of List<Foo>.  But since nothing can be added to this list, there's almost no legitimate reason to want to do that, and one can always cast-and-suppress if that's actually what one needs.



--
guava-...@googlegroups.com.
http://groups.google.com/group/guava-discuss?hl=en
unsubscribe: guava-discus...@googlegroups.com

This list is for discussion; for help, post to Stack Overflow instead:
http://stackoverflow.com/questions/ask
Use the tag "guava".



--
Kevin Bourrillion @ Google
http://guava-libraries.googlecode.com

Olivier Michallat

unread,
Aug 19, 2010, 3:52:17 PM8/19/10
to guava-discuss
Thanks for the clarification.

On Aug 19, 12:53 am, Kevin Bourrillion <kev...@google.com> wrote:
> The wildcard on Lists.transform() was a mistake.  It serves no useful
> purpose; all it accomplishes is that it allows the resulting list to be
> typed as a List<SuperFoo> instead of List<Foo>.  But since nothing can be
> added to this list, there's almost no legitimate reason to want to do that,
> and one can always cast-and-suppress if that's actually what one needs.
>
> On Wed, Aug 18, 2010 at 3:02 PM, Olivier Michallat <omichal...@gmail.com>wrote:
>
>
>
> > Hi all,
>
> > I've noticed a slight inconsistency between Collections2#transform and
> > Lists#transform: the first uses a "Function<? super F, T>", while the
> > latter uses a "Function<? super F, ? extends T>". Did I miss
> > something, or wouldn't it be better to have a consistent signature?
>
> > Allowing "? extends T" is more general, although it requires explicit
> > type parameters when the function returns a subtype:
>
> > Function<String, Integer> f = ...
> > List<String> input = ...
> > List<Number> output = Lists.<String, Number>transform(input, f);
>
> > Olivier
>
> > --
> > guava-...@googlegroups.com.
> >http://groups.google.com/group/guava-discuss?hl=en
> > unsubscribe: guava-discus...@googlegroups.com<guava-discuss%2Bunsu...@googlegroups.com>
Reply all
Reply to author
Forward
0 new messages