As your third paragraph states, if you're grabbing the Nth element out of an 
N-tuple that way, 
you're doing something wrong.  Probably confusing "tuple" with "list".

In some other languages, a method that takes a tuple transparently unpacks the 
tuple into local 
variables, so accessing the Nth element out of the tuple is just a local 
variable access.  This is 
done on the assumption that you're asking for a tuple because you know what 
you're doing, and at 
least the majority of elements are meaningful.  Similarly, the most common way 
to unpack returned 
tuples is through a case statement or multiple assignment, either of which is 
basically the same 
stunt as unpacking into local variables.

The reason Scala has 22 "Tuple" types is because they need some way to hook 
type information onto 
(1,"foo") in a way that's coherent to the Java-based generics model they've got.

~~ Robert.

Josh Suereth wrote:
> I think perhaps as Scala is academic, speed takes more of a focus for 
> some decisions.  Although what you're describing with nested pairs works 
> perfectly well, the performance of grabbing item N off the Tuple is N-1 
> lookups on Pair.   Call it what you will, but Scala tries to strike a 
> balance between elegance and speed.   As you add these higher-level 
> abstractions it can be tough to keep them fast enough for 
> "general-purpose" (or better yet, specific purpose) computing.
> 
> Note that Scala takes the approach for sizes you outline in it's "Seq" 
> abstract Trait.   It is recommended against calling the length method 
> for performance reasons.  If you want fast length calculations, use a 
> List. 
> 
> It seems if you'd like to support an arbitrary number of Tuples, you 
> literally should return Arrays of Objects or some form of Collection, 
> then have javac auto-cast results for you in bytecode.   The difference 
> here would be some form of bytecode flag that says: "This return value 
> isn't a list, it's actually a Tuple of these other types".  Either that, 
> or have the JVM support tuples directly
> 
> -Josh
> 
> On Mon, Feb 9, 2009 at 10:50 PM, Reinier Zwitserloot <[email protected] 
> <mailto:[email protected]>> wrote:
> 
> 
>     If you have inference, you can create TupleXes by chaining tuples.
> 
>     So a (String, Integer, Double) tuple would be:
> 
>     Pair<String, Pair<Integer, Double>>.
> 
>     Then you add a light sprinkling of syntax sugar to make it not look
>     ridiculous.
> 
>     I don't understand why scala doesn't work this way. It would avoid
>     littering the namespace with a tonne of these and having an arbitrary
>     limit of 22.
> 
>     A 'Pair' could then support asking how big it is (it would check if
>     it's second element is a Pair, and if so, return that pair's size +1,
>     and if not, return 2, or better yet, have a BGGA-esque Nothing/Void/
>     Undefined type and don't count those either).
> 
>     On Feb 10, 3:20 am, Josh Suereth <[email protected]
>     <mailto:[email protected]>> wrote:
>      > They're actually unifying Function arguments and Tuples so you
>     can use
>      > them interchangeably (I believe in 2.8).  In java this looks
>      > ridiculuous, as you don't have scala's style of type-inference.  My
>      > guess would be that if tuple support isn't native to the language
>      > currently, it will be in the future.
>      >
>      > Scala sometimes gets the syntactic-sugar wrong where you have to do
>      > things like someFunction((x,y)), but in general it "just works".
>      > It also supports extracting tuples using pattern matching like so:
>      > val (x,y) = someFunction()
>      >
>      > which may look a lot uglier in java:
>      >
>      > (String x, int y) = someFunction();
>      >
>      > but is still rather nice for a "pair" class.
>      >
>      > Also, as a Side note, Tony Morris's project "Functional Java" has a
>      > Tuple class hidden in its bowels of Monads and Theory.  It's actually
>      > called a "Product" (the classes are named PN where N is a number from
>      > 1-8)
>      > Here's the
>     docshttp://functionaljava.googlecode.com/svn/artifacts/2.17/javadoc/index.
>     <http://functionaljava.googlecode.com/svn/artifacts/2.17/javadoc/index.>..
>      >
>      > On Feb 9, 8:48 pm, Michael Neale <[email protected]
>     <mailto:[email protected]>> wrote:
>      >
>      > > Thanks David !
>      >
>      > > 22... interesting...
>      > > How did they do the syntactic support for ( and ) to mean
>     Tuple? Is it
>      > > special or like anything else, just defs?
>      >
>      > > On Feb 10, 11:51 am, David Chuhay <[email protected]
>     <mailto:[email protected]>> wrote:
>      >
>      > > > Scala has tuple classes up to size 22 included in the base
>     langauge
>      > > > package. The (var1, var2, ...) syntax gets compiled into an
>      > > > instantiation of the properly sized Tuple class.
>      >
>      > > > Michael Neale wrote:
>      > > > > So how would you do it in scala ;) ?
>      >
>      > > > > (sorry I always find it crushing how easy everything is in
>     scala, when
>      > > > > I have to look at java).
>      >
>      > > > > Of course you can just do:
>      >
>      > > > > def foo() : (String, Int) = ("Hello", 42)
>      >
>      > > > > never looking into what scala *actually* does with tuples,
>     if its
>      > > > > something nasty or not... (someone else I am sure knows if
>     it is a
>      > > > > good or bad thing).
> 
> 
> 
> > 

-- 
~~ Robert Fischer.
Grails Trainining      http://GroovyMag.com/training
Smokejumper Consulting http://SmokejumperIT.com
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to