Skip to content

Scan/Reduce Overloads & Type Erasure #1881

Closed
@benjchristensen

Description

@benjchristensen

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:

  1. Remove one of several things:
scan(R, Func2<R, ? super T, R>)
scan(Func0<R>, Func2<R, ? super T, R>)
  1. Rename one of them

  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions