monte-language / typhon

A virtual machine for Monte.
Other
67 stars 10 forks source link

Smalltalk-style become: #229

Open MostAwesomeDude opened 3 years ago

MostAwesomeDude commented 3 years ago

Reading old lore on become:, it seems that there's a possible design for one object to safely become another. We can use Promises as an example, since they are transparent forwarders. When a Promise becomes a value via resolution, it delegates all messages to that value, and returns all return values transparently. But any object could do this without Promises:

def becoming():
    var forwarding :Bool := false
    var ref := null
    return object willBecome:
        to run(x):
            return if (forwarding) { ref(x) } else {
                forwarding := true
                ref := x
            }
        match message ? (forwarding):
            M.callWithMessage(ref, message)

This suggests that there is a way to attenuate become: to tame it. An object can become another object precisely when it could choose to delegate all future messages to that object. We could imagine neatly wrapping this delegated ability within var patterns as name patterns for objects:

object var obj:
    to run(x):
        obj := x

This might only work when obj is passed by slot, though, which defeats the point.