There seems to be no way to create a type-safe tuple in Java (I do mean a tuple, with an unlimited number of elements, not a pair). In C++, Boost does offer a good tuples library, but for a limited number of elements, due to language restrictions.
What I have in mind is this:
Which could be accessed in a type-safe fashion. In the following (impossible) code I do miss the non-type template arguments of C++. It would be nice, though, if something like this could be done:
It's impossible to achieve this at the moment. What language features could make this sort of thing possible (ellipsis in generics specifications)? Are any of them being considered? In the meantime, could anyone offer a reasonable alternative?
[ November 26, 2007: Message edited by: Paul Bilokon ] [ November 26, 2007: Message edited by: Paul Bilokon ]
As far as alternatives in Java, the usual idiom would be to create a class with the typed fields you want. Yeah, that's more coding overhead than you probably want here - but that's what Java favors. As you probably know, but I thought it should be said.
Hm, it does seem that it's impossible to do this in Java with type safety (for heterogeneous types) and an unlimited number of fields. If you drop heterogeneous type safely, of course, it's easy - just use an array or List<E>. And you can make tuples with any finite number of fields you want if you make a bunch of classes: Tuple2, Tuple3, Tuple4... (I guess there woudl be Tuple0 and Tuple1 too, as degenerate cases.) Seems like you could also extend this by making the last argument a vararg. So you could have a limited number of heterogeneous types, followed by an unlimited number of one particular type. I realize that's not exactly what you want, but I think it may be the closest you can come in Java.
Here's one possible way that could work. For example a Tuple3Plus would have 3 heterogenous types, followed by a fourth type for all remaining elements:
Maybe that last call should be get(4) instead. There's an annoying inconsistency between accessing the first elements and accessing the varargs; I'm not sure how best to resolve that.
You might want to take a look at Scala, which is another language that runs on the Java Virtual Machine. It supports tuples and strong typing, among other things. It looks like Scala too uses the same basic approach as Boost, creating a finite number of Tuple classes. )See API, specifically scala.Tuple, scala.TupleNN, scala.Product, etc. Without analyzing carefully, I'm going to guess that this means that the limitations that prevent a more general solution in Java are probably at the JVM level, not just the language level. Amusingly, the Scala implementation seems to stop at Tuple22, a rather arbitrary number - but probably way more than needed for most purposes.
I'm not aware of any particular efforts to expand the Java language or JVM in this direction, though that means little as I've never really looked into it. Googling "tuples in java" in quotes, or "java tuples" without quotes, seems to give a number of discussions with varying degrees of usefulness; I only skimmed. This article from Gilad Bracha looks interesting.
I'm a little surprised I don't see a fully developed Java library already out there somewhere similar to Scala's. I bet there are some (again, I only skimmed), but maybe not easy-enough to use to catch on. Maybe there are significant problems with implementing this in Java that I haven't forseen (beyond the apparent impossibility of allowing an unlimited number of elements). Or maybe tuples are just far enough outside standard Java idioms that few people really care.
If you either find existing projects that do this, or go forward with your own, I'd be interested to hear what you find.