Multiple assignment in Scala without using Array?
arrays, functional-programming, pattern-matching, scala, variable-assignment
Solution
Other answers tell you why you can't use tuples, but arrays are awkward for this purpose. I prefer lists:
val a :: b :: xs = "1 2 3 4 5".split(" ").toList
Problem
I have an input something like this: `"1 2 3 4 5"`. What I would like to do, is to create a set of new variables, let `a` be the first one of the sequence, `b` the second, and `xs` the rest as a sequence (obviously I can do it in 3 different lines, but I would like to use multiple assignment). A bit of search helped me by finding the right-ignoring sequence patterns, which I was able to use: ``` val Array(a, b, xs @ _*) = "1 2 3 4 5".split(" ") ``` What I do not understand is that why doesn't it work if I try it with a tuple? I get an error for this: ``` val (a, b, xs @ _*) = "1 2 3 4 5".split(" ") ``` The error message is: ``` <console>:1: error: illegal start of simple pattern ``` Are there any alternatives for multiple-assignment without using `Array`? I have just started playing with Scala a few days ago, so please bear with me :-) Thanks in advance!