I'm trying to "unpack" items from a List
into 4 separate val
s
def myFunc(myList: List[Int]): Unit = {
val (w,x,y,z) = myList
// Compile error
}
If I run this, I get the following error:
Error:(16, 9) constructor cannot be instantiated to expected type;
found : (T1, T2, T3, T4)
required: List[Int]
val (w, x, y, z) = myList
^
So, it looks like the compiler can't infer that everything coming out of a List[Int]
is actually still Int
s. Is there a way to parameterize assignments like this?
The problem is that you're trying to pattern match a list using a 4-tuple extractor. Try this instead:
scala> val a :: b :: c :: d :: rest = List(1, 2, 3, 4, 5, 6, 7)
a: Int = 1
b: Int = 2
c: Int = 3
d: Int = 4
rest: List[Int] = List(5, 6, 7)
scala> val (a, b, c, d) = (1, 2, 3, 4)
a: Int = 1
b: Int = 2
c: Int = 3
d: Int = 4
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments