Generalised plusequals

· coding · Source ↗

TLDR

  • A quarter-baked language experiment proposes an alt keyword that generalizes += to any infix operator, enabling concise immutable deep updates on nested data structures.

Key Takeaways

  • alt x + 1 desugars to x = x + 1; the novelty is extending this to new operators like ]= (list index copy-update) and .= (dataclass field copy-update).
  • alt l[1][1].age.=9 rewrites deeply nested immutable structures without mutating the original object; the compiler expands it into a chain of temporary reassignments.
  • Tilde syntax enables user-defined infix functions: alt l~push~5 appends to l using any plain binary function as an operator.
  • The long-term compile-time plan mirrors Rust’s borrow checker in reverse: assume all structures are immutable, then substitute mutable datastructures where provably safe for performance.
  • The author explicitly references Swift’s mutable value semantics and Rust’s ? operator as partial analogs being generalized.

Hacker News Comment Review

  • Consensus is that this reinvents Haskell lenses: multiple commenters independently identified the pattern as equivalent to lens/traversal combinators without crediting that prior art in the post.
  • A pointed rebuttal emerged around the mutation claim: the alt form on l still reassigns l itself, so the boundary between “not mutating cat” and “mutating l” is arbitrary and unexplained.
  • The q language’s amend operator and the Lil language’s native dot-assignment syntax were cited as working implementations of the same idea, suggesting the concept has prior production use.

Notable Comments

  • @hatthew: argues the post’s framing is inconsistent – alt l[1][1].age.=9 avoids mutating cat but openly reassigns l, so the immutability guarantee is partial at best.
  • @RodgerTheGreat: demonstrates the Lil language handles this with ordinary assignment syntax natively, including nested dict/list updates, with working code examples.

Original | Discuss on HN