soma.cell

E0064: slot_assignment

E0064 slot_assignment — slot = value — a slot is written through its methods.

A compile-time error of soma check, the checker of the Soma programming language (soma explain E0064).

The rule

A memory slot is a Map or a List that is written one entry at a time, so assigning a whole value to it (stock = map(...)) is refused. Writes go through the slot so that its invariants are checked on each entry written. Write the entry with stock.set(key, value) or stock[key] = value, and read it with stock.get(key).

A program that triggers it

wrong.cell

cell Inventory {
    memory {
        stock: Map<String, Int> [persistent]
        invariant stock >= 0
    }
    on restock(sku: String, qty: Int) {
        let current = stock.get(sku) ?? 0
        stock = map(sku, current + qty)
        return current + qty
    }
}

What soma check prints:

$ soma check wrong.cell
error: 'stock' is a memory slot, not a variable — it is never assigned as a whole: write one entry with stock.set(key, value) or stock[key] = value, read with stock.get(key)
  --> wrong.cell:8:9
  |
8 |         stock = map(sku, current + qty)
  |         ^
  = code: E0064
✗ 1 error(s), 0 warning(s).

The fix field of soma check --json: it is never assigned as a whole: write one entry with stock.set(key, value) or stock[key] = value, read with stock.get(key)

On line 8, stock = map(sku, current + qty) becomes stock.set(sku, current + qty).

wrong.cell → right.cell

--- wrong.cell
+++ right.cell
@@ -6,5 +6,5 @@
     on restock(sku: String, qty: Int) {
         let current = stock.get(sku) ?? 0
-        stock = map(sku, current + qty)
+        stock.set(sku, current + qty)
         return current + qty
     }

With the fix, soma check accepts the program:

$ soma check right.cell
✓ All checks passed.