for (i <- Array.apply(1 to 4))
print(i);
Range(1, 2, 3, 4)
Range(1, 10)
//res0: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
So why does val range = Range(1, 2, 3, 4) give error?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A
Rangeis a special kind of collection that is restricted in what it can represent in order to efficiently perform its operations. It is only able to represent a sequence of numbers with a fixed step in between elements. As such, it only needs to be told about the start, end, and step size in order to be constructed. AnArrayon the other can hold arbitrary values, so its constructor must be told explicitly what those values are.The definition of
Range.applyis that it takes either:Here are the definitions of
applyfromscala.collection.immutable.Range:Constrast this with the
applyforscala.Array, which accepts a variable-length argumentT*:If your goal is to have an
Arrayof the numbers 1 to 4, try this: