Repository navigation
Scan/Reduce Overloads & Type Erasure #1881
Copy link
Copy link
Closed
Description
I think the recent additions to scan/reduce may cause issues
scan(R, Func2<R, ? super T, R>)
scan(Func0<R>, Func2<R, ? super T, R>)The Func0 passed in looks like it can be treated like an Object and considered ambiguous and match with R instead of Func0.
Here is a compilation error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project learnrxjava: Compilation failure: Compilation failure:
[ERROR] /Users/benjchristensen/development/github/learnrxjava/src/main/java/learnrxjava/examples/ScanVsReduceExample.java:[10,32] reference to reduce is ambiguous
[ERROR] both method <R>reduce(R,rx.functions.Func2<R,? super T,R>) in rx.Observable and method <R>reduce(rx.functions.Func0<R>,rx.functions.Func2<R,? super T,R>) in rx.Observable match
[ERROR] /Users/benjchristensen/development/github/learnrxjava/src/main/java/learnrxjava/examples/ScanVsReduceExample.java:[10,39] incompatible types: cannot infer type-variable(s) R
[ERROR] (argument mismatch; java.lang.Object is not a functional interface)
[ERROR] /Users/benjchristensen/development/github/learnrxjava/src/main/java/learnrxjava/examples/ScanVsReduceExample.java:[17,32] reference to scan is ambiguous
[ERROR] both method <R>scan(R,rx.functions.Func2<R,? super T,R>) in rx.Observable and method <R>scan(rx.functions.Func0<R>,rx.functions.Func2<R,? super T,R>) in rx.Observable match
[ERROR] /Users/benjchristensen/development/github/learnrxjava/src/main/java/learnrxjava/examples/ScanVsReduceExample.java:[17,37] incompatible types: cannot infer type-variable(s) R
[ERROR] (argument mismatch; java.lang.Object is not a functional interface)
[ERROR] -> [Help 1]
Here is example code: https://github.com/jhusain/learnrxjava/blob/master/src/main/java/learnrxjava/examples/ScanVsReduceExample.java
package learnrxjava.examples;
import java.util.ArrayList;
import rx.Observable;
public class ScanVsReduceExample {
public static void main(String... args) {
Observable.range(0, 10).reduce(() -> new ArrayList<Integer>(), (list, i) -> {
list.add(i);
return list;
}).forEach(System.out::println);
System.out.println("... vs ...");
Observable.range(0, 10).scan(() -> new ArrayList<Integer>(), (list, i) -> {
list.add(i);
return list;
}).forEach(System.out::println);
}
}It looks like we need to do one of 3 things:
- Remove one of several things:
scan(R, Func2<R, ? super T, R>)
scan(Func0<R>, Func2<R, ? super T, R>)-
Rename one of them
-
Add an extra argument so arity solves it.
I actually think the most correct thing to do is remove scan(R, Func2<R, ? super T, R>) since an initial value is most often intended for mutable state in scan/reduce.
cc @headinthebox as this is a last minute API fix we need prior to Monday for 1.0
Reactions are currently unavailable
Activity
headinthebox commentedon Nov 15, 2014
I'd be happy with
Func<0>as the only way, but then we have to do the same forreduceand anything else that takes a seed.headinthebox commentedon Nov 15, 2014
BTW, if we do this, then we should consider generalizing to returning
Ssince we are side-effectingR, so it does not matter to return return the mutable state variable since that is passed in by the implementation.scan(Func0<R>, Func2<R, ? super T, S>)Migrate scan/reduce/collect to use seed factory functions
benjchristensen commentedon Nov 15, 2014
I have submitted #1883 to fix the ambiguity that happens with lambdas.
I have changed
scan,reduceandcollect. I don't see anywhere else that needs to change.benjchristensen commentedon Nov 15, 2014
I'm going to pull the factory overload until 1.1 so we make sure we get this right.
Fix Scan/Reduce/Collect Factory Ambiguity
benjchristensen commentedon Nov 15, 2014
Fixed in #1884 by deleting the ambiguous new factory overloads for
scanandreduceand modifyingcollectto support the mutable case with a seed factory and side-affection action.