Is mixing of data type in a Scala List allowed?

897 views
Skip to first unread message

Vimal Goel

unread,
Dec 3, 2009, 11:56:31 PM12/3/09
to Bay Area Scala Enthusiasts
In Chapter 3, Step 9 of the Odersky book:
"Like lists, tuples are immutable,
but unlike lists, tuples can contain different types of elements."
In the code sequence below, I was successfully able to create lists
with strings and Ints mixed in.
------------------
val myList = List("one", "two", "three", "four")
val hisList = List("five", "six", 237)
val herList = 11 :: 12 :: "seven" :: Nil
val bigList = myList ::: hisList ::: herList
val biggerList = 99 :: bigList
println(biggerList)
------------------
Please explain. Thanks.

Vlad Patryshev

unread,
Dec 4, 2009, 12:10:19 AM12/4/09
to scala...@googlegroups.com
You've created a list of Any, right?

2009/12/3 Vimal Goel <vimal...@gmail.com>

--

You received this message because you are subscribed to the Google Groups "Bay Area Scala Enthusiasts" group.
To post to this group, send email to scala...@googlegroups.com.
To unsubscribe from this group, send email to scala-base+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/scala-base?hl=en.





--
Thanks,
-Vlad

Vimal Goel

unread,
Dec 4, 2009, 12:16:26 AM12/4/09
to Bay Area Scala Enthusiasts
The interpreter says that I have created a List[Any].
I question why the book says you can't mix data types in a Scala List.

On Dec 3, 9:10 pm, Vlad Patryshev <vpatrys...@gmail.com> wrote:
> You've created a list of Any, right?
>
> 2009/12/3 Vimal Goel <vimal.g...@gmail.com>
>
>
>
> > In Chapter 3, Step 9 of the Odersky book:
> > "Like lists, tuples are immutable,
> > but unlike lists, tuples can contain different types of elements."
> > In the code sequence below, I was successfully able to create lists
> > with strings and Ints mixed in.
> > ------------------
> > val myList = List("one", "two", "three", "four")
> > val hisList = List("five", "six", 237)
> > val herList = 11 :: 12 :: "seven" :: Nil
> > val bigList = myList ::: hisList ::: herList
> > val biggerList = 99 :: bigList
> > println(biggerList)
> > ------------------
> > Please explain. Thanks.
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "Bay Area Scala Enthusiasts" group.
> > To post to this group, send email to scala...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > scala-base+...@googlegroups.com<scala-base%2Bunsu...@googlegroups.com>
> > .

Blaine Wishart

unread,
Dec 4, 2009, 1:45:05 AM12/4/09
to scala...@googlegroups.com

This visual presentation of the Scala Unified Types may be useful.
This explanatory text later in the page may also be useful:
The program declares an application UnifiedTypes in form of a top-level singleton object with a main method. Themain method defines a local variable set which refers to an instance of class HashSet[Any]. The program adds various elements to this set. The elements have to conform to the declared set element type Any. In the end, string representations of all elements are printed out.


--

You received this message because you are subscribed to the Google Groups "Bay Area Scala Enthusiasts" group.
To post to this group, send email to scala...@googlegroups.com.
To unsubscribe from this group, send email to scala-base+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/scala-base?hl=en.





--
/blaine

Xuefeng Wu

unread,
Dec 4, 2009, 4:10:10 AM12/4/09
to scala...@googlegroups.com
String and Int are subtype of Any, But they are not Any.
List will ignore the String and Int type.

for example:
 scala> "1,2,3".split(',')
res1: Array[String] = Array(1, 2, 3)

use List
scala> List("1,2,3",4)(1).split(',')
<console>:5: error: value split is not a member of Any
       List("1,2,3",4)(1).split(',')
                          ^

use Tuple
scala> (("1,2,3",4)_1) split(',')
res16: Array[String] = Array(1, 2, 3)


To unsubscribe from this group, send email to scala-base+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/scala-base?hl=en.





--
Scala中文社区:  http://groups.google.com/group/scalacn
Reply all
Reply to author
Forward
0 new messages