Just to clarify, when I say multiple assigment, parallel assignment, destructuring bind I mean the following pattern matching gem
scala> val (x,y) = Tuple2("one",1)
x: java.lang.String = one
y: Int = 1
which assigns "one"
to x
and 1
to y
.
I was trying to do
val (x,y) = "a b".split()
I was expecting that scala would attempt to pattern match the array with the pattern, and would throw a runtime exception if the length of the array wouldn't match the length of the pattern.
All my attempts to easily convert an Array
to a Tuple2
were futile.
scala> Tuple2(Array(1,2):_*)
<console>:7: error: wrong number of arguments for method apply: (T1,T2)(T1, T2)
in object Tuple2
Tuple2(Array(1,2):_*)
^
scala> Tuple2(Array(1,2).toList:_*)
<console>:7: error: wrong number of arguments for method apply: (T1,T2)(T1, T2)
in object Tuple2
Tuple2(Array(1,2).toList:_*)
Is there any neat way to use multiple assignment with arrays or lists?