4 ms·
By default, each message call returns the result of the last expression. the ' ^ something ' syntax is used to explicitly return a value.
by AllegedAlec 7y ago
By 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.
- 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.
- 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.