How can I update (but not add, or append ), items from one list to another in Scala?
It finds me programming in Scala, and I try to update a ListBuffer with the values I have from another list:
val allThemes: ListBuffer[MyType] = ListBuffer[MyType]()
val pageSize = 500
var pageToken = ""
do {
val response: Response = ...
val dataThemes: List[MyType] = response.items // Estos son los nuevos valores
pageToken = response.next_page_token
// Aquí tratataba según de actualizar una lista con
// nuevos valores
if (dataThemes.nonEmpty)
allThemes += dataThemes // Aquí me da un error de tipo
} while (...)
allThemes.toList
But it sends me a type error because it turns out that it doesn't do an update as I thought, but an "append", and therefore the types are different.
type mismatch; found ListBuffer[List[MyType]] Required: ListBuffer[MyType]
I know I can iterate over those values, and then do a +=
, but I don't hear a "fancy", friendly option.
Lol, case closed.
I just read the official documentation, and it mentions that you can do that with a
++=
:https://www.scala-lang.org/api/2.13.6/scala/collection/mutable/ListBuffer.html#++=(xs:scala.collection.IterableOnce[A]):Growable.this.type