4 ms·
I think the key insight of Smalltalk can be grasped just by thinking about how Smalltalk does branching. A line from the GNU Smalltalk tutorial: mybool ifT
by derefr 7y ago
I think the key insight of Smalltalk can be grasped just by thinking about how Smalltalk does branching. A line from the GNU Smalltalk tutorial:
mybool ifTrue: [ ^ 2 / 4 roundup ]
The outer part of this expression reads “send a closure expression, as the message ifTrue, to the receiver mybool.”
Keep in mind, this `mybool` could be anything! It just so happens that there are two runtime objects, `true` and `false`, that satisfy the “Boolean interface” (consisting of messages ifTrue and ifFalse) the way you’d expect—with `true` running any closure passed in an ifTrue message, and `false` running any closure passed in an ifFalse message. (For those of you who know combinatory logic, these objects are essentially the K and SK combinators.)
But consider that you can create your own object that implements this interface—with whatever arbitrary logic you like—and substitute it for a Boolean as the value of `mybool`, and no code will be able to tell the difference.
This is what is meant by “computation as messaging.” Branching is a request to an opaque “branch-evaluating object.” At each step of computation, you’re not getting the runtime to compute—you’re getting your objects to compute. The objects are the computers, and the messages are the instructions to those computers!
- golergka 7y agoThat sounds very interesting. How are return values treated under this model, though? Is the object you send the message to obligated to send something back, synchronously?
- AllegedAlec 7y agoBy default, each message call returns the result of the last expression. the ' ^ something ' syntax is used to explicitly return a value.
- galaxyLogic 7y agoBut the object responding to a message can also "reply later" if one or more of the arguments of the message are "blocks" which are function-like objects created by the sender, having access to variables in the context of the sender. The recipient of a block can call it any time in the future, including multiple times.
- setr 7y agoSo to be clear, it's not like a closure where the local value is "copied" into the closure, but instead it continues to reference the original variable, including any updates to it?
- akkartik 7y agoNo, I don't think that's right. Grandparent just said the closure is returned, and you can call it later to make it block and get a result. Sounds like a future. I'm actually not sure whether Smalltalk passes arguments by value or reference. But they're definitely not constantly updating like spreadsheets or FRP behaviors.
- galaxyLogic 7y agoSmalltalk "blocks" (meaning instances of the class BlockCloures) are much like inner functions in JavaScript, they can read and write variables in the lexical scope where the block(-closure) was defined/created. Are the variables "copied"? I would say so because if you call the containing method multiple times the same code will create new block-closures and they will refer to the variable environment that existed when the block was created, so they will each have their own separate set of outer scope variables. Unlike JavaScript inner functions, Smalltalk closures can cause a return from the containing method (but need not). Here's a good article: http://esug.org/data/Old/vw-tutorials/Adv_VWNotPrintable/Blocks/Advanced_Use.pdf http://esug.org/data/Old/vw-tutorials/Adv_VWNotPrintable/Blo...
- jrochkind1 7y agoAnd, for those interested, ruby blocks were inspired by smalltalk and behave very much like smalltalk blocks. (Although ruby doesn't implement if-condition in this 'pure' way).
- hhas01 7y ago“Are the variables "copied"?” Thinking in terms of C-style variables may be unhelpful. Instead, consider Smalltalk “variables” as scoped names bound to heap-allocated objects. You can bind as many names as you like to a given object at any time, and each name is usable within the bounds and duration of its scope. What the VM does under the hood is more complex than that, natch, but as long as the user-level abstractions don’t leak (which they shouldn’t) the mental model works.
- bonzini 7y agoNo, the default return value is self if the last statement is not "^expression".
- AllegedAlec 7y agoYeah, I'm apparently a complete idiot and forgot basic syntax of a language I have been playing around with for two years... EDIT: This is not sarcasm. I am an idiot and he is right.
- mycall 7y agoIt seems like a consumer\producer event queue to me.
- jrochkind1 7y agohow so?