On Friday, April 10, 2015 at 8:50:00 AM UTC-7, Lukas Eder wrote:
>
> I know it would be great if we could use the compiler even better for
> these kinds of use-cases, but we're limited to what Java has to offer.
> Specifically when using varargs (which are really arrays), this can be a
> bit of a pain. Varargs are used heavily in jOOQ's API for DSL convenience.
> At the same time, varargs suffer from erasure and the lack of reified
> generics and the lack of generic arrays...
>
Yeah, erasure causes many problems.
>
> How about writing this instead:
>
> @SuppressWarnings({"rawtypes", "unchecked"})
> public static <T1, T2, T3> Row3<T1, T2, T3>[] array3(Collection<? extends
> Row3<T1, T2, T3>> collection) {
> return collection.toArray(new Row3[collection.size()]);
> }
>
>
>
OK. That sort-of works and I may do it. It is necessary to also suppress
"SuspiciousToArrayCall"
When I have to tell the compiler to suppress three warnings for a one-line
command, I feel like I'm doing something fishy.
> Or, with Java 8, you could write this:
>
> rowList.stream().toArray(Row3<String, Integer, Integer>[]::new);
>
> At least, I think you can. This unfortunately crashes my Eclipse :-)
>
Doesn't crash my IDE, but no, one can't do it. It won't compile due to
generic arrays.
>
> Unfortunately, ... there is no way to overload based on the generic type
> parameter of the collection. I.e. the following is not possible:
>
> public static <T1, T2, T3> Table<Record3<T1, T2,
> T3>> values(Collection<Row3<T1,T2,T3>> rows) {}
>
>
Actually, I think that *is *possible. My IDE, and standard JRE compiler
allows it. Inside the DSL class you could have a method like this:
public static <T1, T2, T3> Table<Record3<T1, T2, T3>>
values(Collection<Row3<T1, T2, T3>> rows) {
return new Values<Record3<T1, T2, T3>>(rows.toArray(new
Row3[rows.size()])).as("v", "c1", "c2", "c3");
}
You would need to have a series of such methods for "Collection<Row1>" ....
"Collection<Row22>", but that is no different than the current situation
with a bunch of methods for "Row1[]" ... "Row22[]".
In my own code, I avoid java array types, and varargs, exactly because of
the incompatibility of arrays and generics. Using the Collection interface,
or the ArrayList implementation, instead of arrays allows better use of
generics.
Anyhow, passing large amounts of data to SQL using "values" turns out to be
not efficient. We are looking into a variety of other options.
--
You received this message because you are subscribed to the Google Groups "jOOQ
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.