Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!usc!ucselx!petunia!kestrel.edu!gyro From: gyro@kestrel.edu (Scott Layson) Newsgroups: comp.object Subject: Closures in C++ Message-ID: <1991Mar24.020701.27641@kestrel.edu> Date: 24 Mar 91 02:07:01 GMT References: <4578@m5.COM> <1991Mar2.052423.3231@objy.com> <4684@m5.COM> Sender: Gyro@Reasoning.COM Distribution: comp Organization: Kestrel Institute, Palo Alto, CA Lines: 49 I seem to have missed most of the discussion on closures, alas. Since terminology has been an issue, let me briefly summarize the way I am used to using the words (these are informal definitions, but should convey the idea): -- A "closure", to me, is a firstclass procedure object which, loosely speaking, carries its own context with it. -- A "context" is a mapping from variables to values. -- An "instance" is a mapping from operation names to closures, such that all of these closures share a single context. Therefore an instance, to me, is not exactly the same thing as a closure, but they are very closely related: a closure can do only one thing, while an instance can do any of several things depending on which operation is invoked on it. My question is simply this: has there been any discussion on this newsgroup (or anywhere else you are aware of) of the possibility of introducing closures, in this sense, into C++, by decreeing that a pointer to function (horrid bit of terminology, that) shall be implemented as a pair of a code pointer and a context pointer? (Not to say that that's the only possible implementation, but I think it's a quite sensible one.) Just so people have some idea what I'm talking about, a brief example: class C { // constructors etc. omitted int (*f1())(); // member function returning (int (*)()) // (i.e. pointer to function of no args // returning int) private: int f2(); // other member variables omitted }; int (*C::f1())() { return f2; // alternate notation for `&f2' } The idea here is that the thing f1 returns would be like a pointer to function, except that it would perform the f2 operation *on the object on which f1 had been invoked*. -- Scott Layson Burson