E0042: write_to_undefined
E0042 write_to_undefined — an assignment to a name that was never bound.
A compile-time error of soma check, the checker of the Soma programming language (soma explain E0042).
The rule
An indexed or field write such as counts[k] = v changes an existing List, Map or record, so the name must already be bound. Writing to a name that was never declared would fail at run time with "undefined variable", so soma check refuses it. Declare the value first, for example let counts = map() or let rows = [], or fix the name if it is a typo.
A program that triggers it
wrong.cell
cell Orders {
face {
signal units_by_sku(lines: List<Map>) -> Map
}
on units_by_sku(lines: List<Map>) {
for line in lines {
counts[line.sku] = (counts[line.sku] ?? 0) + line.qty
}
return counts
}
}What soma check prints:
$ soma check wrong.cell
error: undefined variable 'counts' — an indexed or field write changes an existing List, Map or record; declare it first (`let counts = map()` / `let counts = []`), or fix the name
--> wrong.cell:7:13
|
7 | counts[line.sku] = (counts[line.sku] ?? 0) + line.qty
| ^
= code: E0042
✗ 1 error(s), 0 warning(s).The fix field of soma check --json: an indexed or field write changes an existing List, Map or record; declare it first (`let counts = map()` / `let counts = []`), or fix the name
The fix
1 line is added after line 5.
wrong.cell → right.cell
--- wrong.cell
+++ right.cell
@@ -4,4 +4,5 @@
}
on units_by_sku(lines: List<Map>) {
+ let counts = map()
for line in lines {
counts[line.sku] = (counts[line.sku] ?? 0) + line.qtyWith the fix, soma check accepts the program:
$ soma check right.cell
✓ All checks passed.