Mutable cells #1660
Labels
C-Moderate Effort
Should take a moderate amount of time to address.
L-Language design
Issues relating to the overall design of the Swarm language.
S-Nice to have
The bug fix or feature would be nice but doesn't currently have much negative impact.
Z-Feature
A new feature to be added to the game.
Z-Research
This issue requires inventing something new and/or studying research papers for clues.
It's a little hard to believe we don't already have an issue for this, but I couldn't find one. It would be fun to add mutable reference cells to the language, so you could do some imperative programming when convenient. Our CESK machines already track a mutable store, so this really just requires adding some surface language features that allow accessing it directly.
Proposal
In particular, we could add the following primitives:
Ref a
is the type of references to a mutable cell holding a value of typea
ref : a -> Cmd (Ref a)
creates a new mutable cell holding the given valueread : Ref a -> Cmd a
reads the current value of a mutable cellwrite : Ref a -> a -> Cmd Unit
writes a new value to a mutable cellWe could optionally use special syntax for
read
andwrite
, e.g.!r
forread r
andr := x
forwrite r x
. It may be OK to get rid of theCmd
on some or all of the above primitives (particularlyread
); that would require some careful thought/research to make sure it doesn't break type safety somehow.Additional considerations
Ref a
value to another robot (e.g. via Inter-robot communication #94 ) should result in the cell being copied, not in a shared reference to the same mutable cell. Or a value of typeRef a
could contain not just a memory address but also a robot ID, so we can tell when we are trying to access memory stored on another robot (with a suitable exception being thrown in that case).The text was updated successfully, but these errors were encountered: