C++; Java; Algol 68; C; OCaml; Scheme;——Keykos
I consider the C++ and Java samples to be the newer class based pattern.
The Algol 68 and C versions don’t work because the storage for variable count does not survive the invocation of cc.
The point of the exercise is that their syntax and most of their semantics supports the pattern.
The C syntax uses a gcc extension to C, supported by many compilers.
The Algol68 interpreter, whose diagnostic follows the program, checks expired scopes at runtime.
This construction is specifically supported by Scheme and OCaml.
Java and C++ have other native syntaxes for initialization, but they rely more of the language definition.
I prefer the patterns used in Algol 68, C, OCaml, Scheme as they do the job with less conceptual overhead. It can be argued that the compiler has a new job to discover that the variable count cannot be an ordinary stack variable. The AWRE Algol 68 compiler did just that and this program was valid there.
An advantage of the closure pattern, (OCaml & Scheme) is that the caller of cc can delegate the two resulting functions to two separate deputies and know that the first deputy cannot read the counter and that the second cannot increment it. I do not know how to do this easily in the Java or C++ pattern.
The class patterns have an advantage in sibling access where an object of the class is passed to a function in the class. The function can see inside the object. My closure pattern here does not support that without the sealer-unsealer primitive or some other form of synergy.
I do not see how to do this in Apple’s block construct which produces just one function per new environ.
Data abstraction is: