Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!shelby!neon!craig From: craig@Neon.Stanford.EDU (Craig D. Chambers) Newsgroups: comp.lang.modula3 Subject: Re: Closures in m3 Message-ID: <1991Feb22.232418.29567@Neon.Stanford.EDU> Date: 22 Feb 91 23:24:18 GMT References: <91Feb21.152416pst.16910@alpha.xerox.com> Organization: Stanford University Lines: 34 In article <91Feb21.152416pst.16910@alpha.xerox.com> Mike_Spreitzer.PARC@xerox.com writes: >You are right about the first point: local procedures can't escape (given M3 as >it is). So the closure scheme is more convenient in some cases. It's less >convenient in others (where the local procedure would work, making the local >procedure involves less overhead than making a whole closure object). Wouldn't >the local procedure scheme suffice in the vast majority of actual uses? Note >that if there >were a syntax for objection creation that allowed method literals, the closure >scheme can also approach the elegance of CLU iterators. In fact, full closures (or even downwards-only closures) are *more* powerful than CLU iterators (if this is what you meant by "elegant"). CLU iterators effectively take a single closure argument (the body of the for loop). If closures are first-class, then iterators can take multiple closure arguments. In our Self system we have many methods that take multiple closure arguments. For example, one iterator method called "doFirst:Middle:Last:" takes a closure to execute for the first element of the receiver collection, a different closure for the interior elements, and a third closure for the last element of the collection. This is useful for instance when constructing a string representation of the collection and needing to insert commas in between elements; the code can insert a comma before all the elements except the first by using a different closure for the first element and the other elements. One thing that makes closures usable in practice is a concise closure literal syntax. Since Self and Smalltalk both rely on closures and messages for all control structures, even ifs and whiles, this concise syntax had to be developed just to allow the language to be used at all. Without it, I wouldn't expect closures to receive much use. This lack of concise syntax in Pascal-like languages has probably been a major hurdle to widespread use of closures. -- Craig Chambers