Вопрос

In the following code, inside the for comprehension, I can refer to the string and index using a tuple dereference:

val strings = List("a", "b", "c")
for (stringWithIndex <- strings.zipWithIndex) {
  // Do something with stringWithIndex._1 (string) and stringWithIndex._2 (index)
}

Is there any way in the Scala syntax to have the stringWithIndex split into the parts (string and index) within the for comprehension header, so that readers of the code do not have to wonder at the values of stringWithIndex._1 and stringWithIndex._2?

I tried the following, but it would not compile:

for (case (string, index) <- strings.zipWithIndex) {
  // Do something with string and index
}

Нет правильного решения

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top