Ivan Reese
Kartik Agaram
Beni Cherniavsky-Paskin
10/08/2024, 9:01 AMtell
(aka ask
) mechanism for "OOP": It simply takes a piece of code to execute in another context. E.g. if you have 2 turtles, you can tell turtle2 right 90
(the inner code e.g. right 90
can be wrapped in a box but that's optional for short things). Pedagogically, this allows students to operate multiple stateful copies, without having to first learn encapsulation, interfaces, and having to expose a "method" for whatever you want to tell them to do (which requires being comfortable with defining functions).
The only other language I've seen with such model is Snap! (see "Sending Messages to Sprites").
[Well, does JS depracted with
statements count? But JS has real methods too, plus it lacks an expression form.]Beni Cherniavsky-Paskin
10/08/2024, 9:02 AMtell
context.) This is rather complex.
• In Boxer, local variables wouldn't solve this either, all name lookups are affected by tell
. This is consistent with Boxer's "copy and execute" mental model of how calling a doit box works. (Opt-in lexical scope is possible via ports, but that's really an advanced escape hatch.) So, how do you pass a block of code to be executed in new context yet parametrize it from current context?! Boxer added a special ^
syntax just for that:
For example, ASK JOE FORWARD X has JOE use his X, while ASK JOE FORWARD ^X has JOE use the X available where ASK appears.
— from "Boxer Structures"So, syntactically
^X
etc looks kinda like templating [btw Boxer has a generic "semiquote" too — build
, with a different syntax].
(Semantically, this is not really templating but a 2nd runtime context Boxer interpreter keeps track of)
Like all templating, it'd get horrific if you need more levels deep...
In regular OOP, this Just Works when you do something like arrow.moveTo(this.x, this.y)
— the argument expressions are computed in the caller's environment, then method body is evaluated in target's environment. The call boundary is also the this
switch boundary! And one can become productive before having to think too deep about that.