Tuples?

Gilad Bracha just writes an article on Java tuples support, in which he claims that every language should have it. While we all know there's even no Pair inside Java, why comes him suddenly saying that Java must have it? Maybe he's struggled for it for many years but always turned down by Graham Hamilton? Now that Graham himself also left Sun, and Gilad believes he can pick up this topic again? Just kidding.

n-Tuple is nice and I also like it. Some people commenting on Gilad's post want it be implemented in JVM because that will help the performance. I don't like the idea, partly because I don't like JVM changes, which means a whole bunch of operators on the new data type, but mostly because I think Tuple is just another language element, and must be a generic class. What's more important is how to express it in an elegant style so that it looks natural, slim, and easy to use.

The following is my proposal:

 

  1. First create a new immutable class Pair<A,B> (in the java.util package), which has a constructor with 2 arguments, and have 2 methods calls car() and cdr().
  2. Tuple<A,B,...,C> is syntactic sugar/macro of Pair<A,Pair<B,Pair<...,Pair<C,Void>...>>. Javac knows this.
  3. Javac also knows that new Tuple<A,B,C>(a,b,c) is new Pair<A,Pair<B,Pair<C,Void>>>(a,new Pair<B,Pair<C,Void>>(b, new Pair<C,Void>(c, null))), and it writes this translation much faster than I do.
  4. and .caddr means .cdr().cdr().car(), and it automatically finds out if you write too many d at compile time.
  5. Maybe tuple knows about variant arguments, but seems not necessary.
Is this enough?
Comments:

No, that's not enough, as it doesn't handle the subtyping rules correctly.

Posted by Neal Gafter on March 08, 2007 at 01:47 PM CST #

Er... How about this?
        //Tuple<? extends Object, ? extends A, ? extends B> t = new Tuple<A, B, C>(new A(), new B(), new C());
        Pair<? extends Object,Pair<? extends A,Pair<? extends B,Void>>> t = 
                new Pair<A,Pair<? extends A,Pair<? extends B,Void>>>(new A(),
                    new Pair<B,Pair<? extends B,Void>>(new B(),
                        new Pair<C,Void>(new C(),null)));
        //Tuple<? super B, ? super C, ?> t = new Tuple<A, B, C>(new A(), new B(), new C());
        Pair<? super B,Pair<? super C,Pair<?,Void>>> t3 = 
                new Pair<A,Pair<? super C,Pair<?,Void>>>(new A(),
                    new Pair<B,Pair<?,Void>>(new B(),
                        new Pair<C,Void>(new C(),null)));
The translation now uses info from both the type declaration and the constructor.

Posted by Weijun on March 09, 2007 at 10:16 AM CST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

This blog has a comments managing system that requires me to approve each comment manually. Please do not re-post and I will reply it (if I have an answer) when I get pinged.

Search

Top Tags
Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today