3 ms·
So 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, includi
by setr 7y ago
So 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.
- TeMPOraL 7y agoWhat closures are you used to? Proper closures capture local variables and keep them available, they don't copy anything.
- rienbdj 7y agoIn C++ they can be copied.