3 ms·
Smalltalk "blocks" (meaning instances of the class BlockCloures) are much like inner functions in JavaScript, they can read and write variables in the lexical s
by galaxyLogic 7y ago
Smalltalk "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.