On 22 Apr., 15:10, Lance Walton <
lance.c.wal...@googlemail.com> wrote:
> On 22 Apr 2011, at 08:53, Christian Semrau wrote:
> > is there a specific reason why FunctionalPrimitives.zip() has this
> > signature?
>
> > public static List zip(final Iterable<List> lists);
>
> This is because the Lists that zip produces can contain arbitrary types. e.g. you could zip(list(list(1, 2, 3), list("a", "b", c")))
> and then the result list would be list(list(1, "a"), list(2, "b"), list(3, "b")).
I see your point. Zips of different types are difficult to make
typesafe. Surely someone somewhere did already come up with a usable
Java signature?!
The original zip variant does not compile when used with the any one
of these source lines:
zip(list(list(1, 2, 3), list("a", "b", "c")));
zip(list(list(1, "a"), list(2, "b"), list(3, "b")));
It seems that the problem of mixed types is not the zip function's
alone, but also the list function's: What is the type of the following
expression?
list(list(1, 2, 3), list("a", "b", "c"))
The original variant works when using a raw cast:
public static List zip(final Iterable<List> lists); // original
List result = zip(list((List)list(1, 2, 3), list("a", "b", "c")));
The typed variant also works with a cast:
public static <T> List<List<T>> zip(final Iterable<List<T>> lists); //
typed
List<List<Object>> result = zip(list(list((Object)1, 2, 3), list("a",
"b", "c")));
List<List<Serializable>> result = zip(list(list((Serializable)1, 2,
3), list("a", "b", "c")));
Signatures that seem to work without casts for mixed types are these:
public static List zip(final Iterable lists); // raw
public static List<List<?>> zip(final Iterable<? extends List<?>>
lists); // wildcards
public static <T> List<List<T>> zip(final Iterable<? extends List<?
extends T>> lists); // extended-type
The extended-type variant must be invoked with explicitly stated type
parameter if one wants to assign the result to a variable, like so:
List<List<Object>> result = FunctionalPrimitivesTest.<Object>
zip(list(list(1, 2, 3), list("a", "b", "c")));
The type Object could be replaced by Serializable or Comparable<?> --
these are all the common super classes and interfaces of String and
Integer.
This would be the most flexible variant, but it usually cannot be used
when statically imported due to the explicit type parameter.
The wildcards version has the most general parameter type, but its
return type is difficult to work with.
So, as a first step towards a better signature, how about this?
public static List zip(final Iterable<? extends List<?>> lists); //
wildcard argument with raw result
> I think you should have commit rights :-) Want to join the project?
>
> Regards,
> Lance
Thank you for the invitation. I'll be glad to join.
Christian