Tuples- Returning multiple values in Scala
When I was coding in Java I used to build Classes just to return multpile values and also sometimes used pass by reference (by means of using Objects). I really missed a permanent solution
Scala has a solution for this- It supports something called “Tuples” which is created with the literal syntax of a comma-separated list of the items inside parentheses like (x1,x2,x3 …). The items in the parantheses may not be related to each other in terms of the type, which means that we can have String’s, Int’s and so on. These literal “groupings” are instantiated as scala.TupleN instances, where the N is the number of items in the tuple. The Scala API defines separate TupleN classes for N between 1 and 22, inclusive. Tuples can be assigned to variables, passed as values or return them from the methods.
Let me explain this with an example (I have used the interactive mode of scala command):
scala> val tple=("Hello World",56, 56.66, true)
tple: (java.lang.String, Int, Double, Boolean)= (Hello, Int, Double, Boolean)
The above code creates a tuple of 4 elements of totally unrelated types. It can be clearly seen in the result printed soon after the statement.
scala>println(tple._1) Hello World scala>println(tple._4) true
It has to be noted that the parameter are referenced from 1 and not from 0. So its pretty straight forward- use <Tuple Name>._<Parameter Position> to access the element.
There are different ways of creating Tuples. Some of them are:
scala>Tuple4(5,6,"Hello",6.5) res0:(Int,Int,java.lang.String, Double)=(5,6,"Hello",6.5) scala>val tple1=1->2->"Hi" tple1: ((Int,Int),java.lang.String)=((1,2),"Hi")
Observe that the tple1 contains another tuple inside it- (Int,Int). Use the same idea to access it.
scala>println(tple1._1._1)1
This was in brief about Tuples. This concept caught my attention immediately as i had faced the problem in Java. So thought of describing it here.
No related posts.
One Response to Tuples- Returning multiple values in Scala
Connect to us …
Archives
- May 2013 (9)
- April 2013 (6)
- March 2013 (6)
- January 2013 (5)
- November 2012 (2)
- September 2012 (1)
- July 2012 (5)
- June 2012 (1)
- May 2012 (4)
- April 2012 (7)
- March 2012 (2)
- February 2012 (4)
- December 2011 (2)
- November 2011 (4)
- October 2011 (2)
- September 2011 (1)
- August 2011 (1)
- July 2011 (1)
- June 2011 (1)
- May 2011 (1)
- April 2011 (1)
- February 2011 (4)
- December 2010 (3)
- November 2010 (2)
- September 2010 (2)
- August 2010 (3)
- May 2010 (2)
- March 2010 (6)
- December 2009 (1)
- November 2009 (3)
- July 2009 (6)
- June 2009 (3)
- May 2009 (1)
- April 2009 (6)
- March 2009 (1)
- January 2009 (1)
- December 2008 (8)
- November 2008 (5)
- October 2008 (6)
- September 2008 (4)
- August 2008 (8)
- July 2008 (19)
- June 2008 (29)
- May 2008 (27)
- April 2008 (11)
- March 2008 (8)
- February 2008 (22)
- January 2008 (3)
Send To Readmill
Send to ReadmillRecent Posts
- Parsing XML using DOM, SAX and StAX Parser in Java
- How to create ADF TreeTable programmatically?
- Book review: The Object-Oriented Thought Process
- How to show links in ADF Messages
- Runtime Polymorphism in Java
- Understanding RowKey values in ADF TreeTable
- Train Wreck Pattern – A much improved implementation in Java 8
- Converting a List into comma separated value string in Java
- A simple application of Lambda Expressions in Java 8
- Template Method Pattern- Using Lambda Expressions, Default Methods
Disclaimer
Some of the links contained within this site have my referral id, which provides me with a small commission for each sale. Thank you for your support.










This is horrible. Tuples are a basic type in most modern languages, since they are a basic type in calculus, which is a good starting point to design a language (check haskell and lambda calculus). The fact that tuples are not a standard class in Java is horrible and very annoying, and so it is the fact that some programmers may need clarification about what a tuple is, as well as Scala being considered any innovative for including tuples.
LISP has been around for a while, it’s a powerful and nice language with a horrible (uber-parenthesized) syntax, have we learnt nothing from it?