scala/scala-java8-compat

Provide a Tuple type

huntc opened this issue · 2 comments

huntc commented

Consider providing a Java implementation of a Tuple - perhaps even just with an arity of 2 given my assertion that it is the most commonly used. I see that Java developers will be rolling their own tuple type, so we could help them avoid that while also facilitating our Scala <> Java translations.

Here's Play's version of Tuple:

/**
 * Play's Tuple class given Java 8's lack of it.
 */
public class Tuple<A, B> {
    public final A _1;
    public final B _2;

    public Tuple(A var1, B var2) {
        this._1 = var1;
        this._2 = var2;
    }

    public String toString() {
        return "Tuple2(_1: " + this._1 + ", _2: " + this._2 + ")";
    }

    public int hashCode() {
        byte var2 = 1;
        int var3 = 31 * var2 + (this._1 == null?0:this._1.hashCode());
        var3 = 31 * var3 + (this._2 == null?0:this._2.hashCode());
        return var3;
    }

    public boolean equals(Object var1) {
        if(this == var1) {
            return true;
        } else if(var1 == null) {
            return false;
        } else if(!(var1 instanceof Tuple)) {
            return false;
        } else {
            Tuple var2 = (Tuple)var1;
            if(this._1 == null) {
                if(var2._1 != null) {
                    return false;
                }
            } else if(!this._1.equals(var2._1)) {
                return false;
            }

            if(this._2 == null) {
                if(var2._2 != null) {
                    return false;
                }
            } else if(!this._2.equals(var2._2)) {
                return false;
            }

            return true;
        }
    }
}

If you're going to pull in the entire Scala library anyway, why not use scala.Tuple2? And if you're not, why would you be using scala-java8-compat? The only advantage would be that in Java one could use _1 and _2 instead of _1() and _2(), with the drawback that extra conversion functions are needed any time the Java 8 Tuple2 interacts with Scala anything.

huntc commented

Yes, I didn't think that through. Thanks.