3 ms·
At the vm-local level (i.e., not fork -> process memory COW, but rather immutable & persistant objects), you can make objects immutable via their metatables: se
by silentbicycle 16y ago
At the vm-local level (i.e., not fork -> process memory COW, but rather immutable & persistant objects), you can make objects immutable via their metatables: set __index and __newindex to functions that do proxying. The former would lookup fields from the appropriate object instance, and the latter would set the (internal) object reference to a new copy with the updated field.
Come to think of it, that's almost a direct Lua translation of what you said: "In VisualWorks Smalltalk, I'd just start off by setting objects immutable and using a top-level exception handler on the mutation exception. Alternatively, one could also use forwarder proxies." The __newindex metatable field is a mutation hook and __index is like doesNotUnderstand. (Lua seems more like Self than Smalltalk, though.)
But, I don't think I'd do it that way - when going for immutable & message-passing semantics, adding syntactic sugar to make it look like stateful OOP seems misleading to me. You can stream plain Lua tables* over sockets pretty easily, though, so message-passing is probably much simpler. You can stream Lua functions, but not coroutines. (Streaming functions with closures takes a bit of debug API hackery, though.)
* Lua tables are almost identical to JSON.
FWIW, there's a port of Erlang's message-passing concurrency and supervision hierarchy programming model to Lua: ConcurrentLua (http://concurrentlua.luaforge.net/ http://concurrentlua.luaforge.net/). I've only toyed with it a little bit, since I didn't really know Erlang at the time. It uses co-operative multitasking (coroutines), has localized mutable state (doesn't force modify-once variables within processes), and doesn't have selective receive (yet, though I keep meaning to add that). I'd rather just use Erlang for fault-tolerant-server projects (possibly w/ Lua behind ports), but it seems like a good library for distributed programming in Lua.