If you just want multiple return/assignment there are quite a few ways to deal with the problem. One common way is to try to just push multiple values on the stack. But I think the design of the current JVM pretty much limits you to using TupleN classes, a list structure, or arrays. Still, there are advantages to TupleN classes. For example the classes can support methods beyond just unpacking the fields, instances can be used via non-Java JVM languages without necessarily changing those languages, etc.
By the way, Scala makes tuples pleasant by sprinkling a tiny bit of syntactic sugar over TupleN classes so that (a,b,c) syntax works as a shortcut for Tuple3(a,b,c). But it turns out that multiple assignment in Scala is just one case of a general construct in the language. You can write case class Person(id : Int, name : String, age : Int) def lookupPerson(id : Int) : Person = // some person lookup code val Person(_, name, age) = lookupPerson(1) println("person with id 1 has name " + name + " and age " + age) This code 1) Declares that we want to be able to unpack and match instances of Person (that's what "case class" means) 2) Looks up the person 3) Unpacks the resulting person into the name and age variables (the underscore says skip the id since we already know it). 4) Prints the name and age In Scala the multiple assignment val (a,b,c) = (1,2,3) is just sugar over val Tuple3(a,b,c) = Tuple3(1,2,3) And that in turn is just one case of a generalized pattern matching mechanism that's available to you and your code as shown in the Person example. On Feb 22, 6:29 am, -jn- <joel.ne...@gmail.com> wrote: > Not so much. > > On Feb 17, 10:12 am, Reinier Zwitserloot <reini...@gmail.com> wrote: > > > To summarize Joel Neely's argument: > > > "I think java should have tuple packing and unpacking exactly as > > python has it". > > I'd have summarized it more as "multiple assignment doesn't require a > full implementation of Tuples and structural typing, and offers > additional notational conveniences." > > I didn't write in terms of tuples and Python (or Perl, Ruby, etc.) > deliberately, to avoid the excess baggage. Although use of anonymous > tuples is one way to implement what I described (and a nice one at > that), it isn't the only way. (Just as "hidden go to statements" > aren't the only way to think about, or implement, iterative program > structures.) > > Dijkstra discussed "concurrent assignment" on p. 28 of /A Discipline > of Programming/ (copyright 1976), and the concept was already well > known by then. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to javaposse@googlegroups.com To unsubscribe from this group, send email to javaposse+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/javaposse?hl=en -~----------~----~----~----~------~----~------~--~---