Reputation: 3055
Currently, I am learning Scala and reading this book Programming in Scala and which says, " Unlike an array or list, a tuple can hold objects with different types." For example, the following tuple contain Int, String and Float.
val tup = (1, "hello", 4.4)
Again, the book say, "If you want to have any type of element in list/array, then you can use Any Datatype."
val list = List[Any](1, "hello", 4.4)
So, what is the difference between above these 2 approaches? what are the benefit of one over another?
Upvotes: 7
Views: 7889
Reputation: 739
I don't agree with PawelN
val list = List[Any](1, "hello", 4.4)
for (i <- list) println(i.getClass)
or
val array = Array[Any](1, "hello", 4.4)
for (i <- array) println(i.getClass)
results in:
class java.lang.Integer
class java.lang.String
class java.lang.Double
Hence, no casting is needed when you access an item in a List or an Array of Any. Of course, I'd be suspicious of design of code that is using such a thing.
The main point of a Tuple, is for a function to be able to return arbitrary numbers of objects of different types. This is much lighter weight then creating a class every time you need to return multiple values at the cost of some type safety.
Upvotes: -1
Reputation: 2276
Any
is a data-type , just like Int
or String
, but different from them.
Tuple
is a container, which can hold multiple data-types, i.e. it can contain vals of different data-types, but the type of the Tuple
will depend upon how many elements are there in the Tuple
, so for example:
val tup = (1, "hello", 4.4) // type of tup here is scala.Tuple3 (Int, String, Double)
val tup = (2.3, null) // type of tup here is scala.Tuple2 (Double, Null)
val tup = (5:Any, "hello", 2.2) // type of tup here is scala.Tuple3 (Any, String, Double)
But the type of each of the elements in the Tuple
will be maintained. Any
on the other hand, is like a homegenous data-type in which there's no unique type identity of the elements, be it a String
or Int
or Null
type initially, will be converted to a single data-type Any
and will lose all type-information.
Update:
The difference between a Tuple
and a List[Any]
is that a Tuple
can hold elements of multiple data types, still maintaining the data type of the individual elements.
While a List
or Array
can only hold elements of a single data type, so a List[Any]
will consist of all elements of type Any
, so it'll basically convert all the elements (irrespective of their earlier data-type) to Any
.
Upvotes: 9
Reputation: 848
Tuples are type safe and with List[Any] you have to cast element to appropriate type.
val tup = (1, "hello", 4.4)
tup._2 --> gives you string
val list = List[Any](1, "hello", 4.4)
list(1) --> gives you object of type Any and you have to cast this object
Your tuple is a class of type Tuple3[Int, String, Double].
Upvotes: 3
Reputation: 170745
tup
has type (Int, String, Double)
, so you can get data back with its correct type: tup._1
has type Int
. list
has type List[Any]
, so you've lost all type information: list(0)
's type is Any
.
Don't use Any
(or List[Any]
, etc.) unless you have to; certainly don't use it when a tuple will do.
Upvotes: 13