The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Why parameterize Array?

2 replies on 1 page. Most recent reply: Dec 9, 2010 8:33 PM by Jim Balter

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
maninder batth

Posts: 4
Nickname: anywhere
Registered: Mar, 2006

Why parameterize Array? Posted: Apr 6, 2010 2:25 PM
Reply to this message Reply
Advertisement
Hi, i was reading the book programming in scala and came across the example, where the author parameterizes the Array to be of String type as following :-
val greetStrings = new Array[String](3)
I was wondering if the parameterization is necessary. Arrays by definition can only contain ONE type. hence, the type of an array could be inferred by element at 0 index.
greetStrings(0) = "string" Or greetStrings(0) = 1


maninder batth

Posts: 4
Nickname: anywhere
Registered: Mar, 2006

Re: Why parameterize Array? Posted: Apr 6, 2010 2:29 PM
Reply to this message Reply
Clarification :- it doesnt have to be 0th index. That was used to serve an example. It could be any index for that matter. But when inserting new data into an Array that already contains data, the type could be inferred and an exception can be raised if multiple types need to be inserted.

Jim Balter

Posts: 13
Nickname: jibal
Registered: Dec, 2010

Re: Why parameterize Array? Posted: Dec 9, 2010 8:33 PM
Reply to this message Reply
Scala is a *statically* typed language; the type of the array (and of the val it is assigned to) must be known at compile time.

Flat View: This topic has 2 replies on 1 page
Topic: Exercises in the second edition? Previous Topic   Next Topic Topic: page 241, compile error (pre-print version of the 2nd edition)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use