Replace all content in a List with another List.
← Operators and Expressions · Ref: Q1078
Replace with :^: — target gets all source items:
tasks :^: newSprint
The target list is cleared and filled with the source's items. The source is unchanged.
See Q1075 for :=: copy. See Q1077 for :~: merge.
Example
defines module qa.operators.replacerecordcontents defines class Task name <- String() priority <- Integer() Task() -> name as String priority as Integer this.name :=: name this.priority :=: priority default operator defines program ReplaceRecordContentsDemo() stdout <- Stdout() //Current task list tasks <- List() of Task tasks += Task("Fix login bug", 1) tasks += Task("Update docs", 3) stdout.println(`Current tasks: ${tasks}`) //New sprint tasks to replace the current list entirely newSprint <- List() of Task newSprint += Task("Deploy v2.0", 1) newSprint += Task("Performance audit", 2) newSprint += Task("Add monitoring", 3) //Replace all current tasks with the new sprint tasks :^: newSprint stdout.println(`After replace: ${tasks}`)
Common mistakes
E50060 — EK9 List has no 'addAll' method — use the ':^:' replace operator to overwrite a collection's contents in place. See ek9 -h E50060 for details.
Incorrect:
tasks.addAll(newSprint)
Correct:
tasks :^: newSprint
Other ways to ask this
- Overwrite a list entirely using :^:
- Swap the contents of one collection with another
- In Java I'd clear() then addAll(). How do I replace list contents in EK9?
- I need to replace all items in a list with items from a different list
Coming from another language?
Java: list.clear(); list.addAll(other). Python: target[:] = source. Go: target = make([]T, len(source)); copy(target, source). EK9: target :^: source replaces contents in place.
Keywords: collection, replace, overwrite, list, swap contents, :^: