question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FuncN generic varargs parameter

See original GitHub issue

As mentioned [1] [2] it would be nice for Observable.zip(iterable, funcn) to handle iterables of the same type, without having to go about casting the object to an integer for making something like this to work

List<Observable<Integer>> iterable = Arrays.asList(Observable.from(1), Observable.from(2), Observable.from(3));
Observable<List<Integer>> observable = Observable.zip(iterable, Arrays::asList);

Making FuncN<R> into FuncN<T, R> kind of snowballed on me as I reached the OperatorZip$Zip which seems to be needing the Object type.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
laurilehmijokicommented, Jun 17, 2016

This is how we solved the problem in Kotlin:

fun <T, R> List<Observable<T>>.combineLatest(combineFunction: (List<T>) -> R): Observable<R> = 
        Observable.combineLatest(
                this,
                { xs: Array<out Any> ->
                    combineFunction(xs.toList() as List<T>)
                }
        )
2reactions
bobvanderlindencommented, Apr 4, 2016

I came across this same issue, but with combineLatest. I see how it causes an incompatibility, but I’d like this to be reconsidered for v2.

Currently I’m using the following overload of combineLatest:

public static <T, R> Observable<R> combineLatest(List<? extends Observable<? extends T>> sources, FuncN<? extends R> combineFunction)

The definition of FuncN is:

R call(Object... args);

Firstly, the type information gets lost. A user would presume T is passed, but it is actually Object. This caused quite a bit of confusion when using RxJava in Kotlin, where all types can be implicitly resolved and the end of an chain would result in some form of Object. Going back up the chain to see what function had misbehaving type information resulted in combineLatest.

Additionally, I personally find the use of varargs a bit confusing. I passed in a List and I assume a List is passed to the combineFunction. Instead it is an Array/[], but it is not explicitly typed as an array (as it is typed using varargs). I see how varargs would fit combineFunction if combineLatest was called with varargs, but in this particular overload it is not.

It might be a good idea to introduce the following function definition for V2:

public static <T, R> Observable<R> combineLatest(List<? extends Observable<? extends T>> sources, Func1<List<? extends T>, ? extends R> combineFunction)

That way a list is passed into combineLatest and a list is passed to the combineFunction. That way, no type information is lost.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java generics and varargs - Stack Overflow
The intention here is to assert that all parameters passed to this function are Class objects extending the Class given as the first...
Read more >
Item 32: Combine generics and varargs judiciously - InformIT
Recall that a generic array is created when the method is invoked, to hold the varargs parameters. If the method doesn't store anything...
Read more >
Effective Java! Combine Generics and Varargs Judiciously
If we create a function that takes a non-reifiable varargs parameter we are presented with a warning talking about Possible heap pollution ....
Read more >
Fixing ugly Java APIs: read-only generic varargs
"unchecked generics array creation for varargs parameter" ... declared as covariant automatically, which makes them read-only inside the varargs function.
Read more >
Java Varargs with examples - CodeGym
In case you forgot, this is a variable-length argument. ... Unchecked generics array creation for varargs parameter. What does that mean?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found