5 ms·
It is interesting that B. Stroustrup first looked at Algol 68 as a potential base for introduction of classes into a statically compiled programming language, a
by Koshkin 8y ago
It is interesting that B. Stroustrup first looked at Algol 68 as a potential base for introduction of classes into a statically compiled programming language, and then he turned to C - probably because Algol 68 was already as complex as is now C++.
- Taniwha 8y agoactually modern C and Algol68 (which doesn't have objects) are roughly equivalent in complexity .... probably the obly thing that C doesn't have is the type-case (algol68 unions have a hidden small enumerator that describes the actual type stored within the union)
- int_19h 8y agoAlgol-68 also had GC, operator overloading, nested functions (which were full-fledged closures), and lambdas.
- bakul 8y agoI believe the original Algol-68 didn't have full closures. For example, the following (taken from the revised report) wouldn't work: proc f = (real x) proc (real) real: (real y) real: x + y; proc (real) real g := f (3); As by the time g is created, x of f has disappeared from the stack.
- Taniwha 8y agoWhat it does have is the algol 60 idea of nested scopes For example if proc A contains a variable X and declares a subroutine B within it, then B can access X, even if you take a pointer to B and call it from elsewhere (effectively that pointer to B also carries info about which instance of A it came from .... These days we've largely forgotten about this idea of how variables should be accessed and inherited between subroutines - hardware designers used to design support for these language features (display registers in Burroughs mainframes) - to be fair as a feature it honestly doesn't add a lot of practical functionality considering what it costs
- int_19h 8y agoWe even have the legacy of that in x86 - the ENTER opcode, the second argument for which is "nesting level of the function": "If the nesting level is 0, the processor pushes the frame pointer from the BP/EBP/RBP register onto the stack, copies the current stack pointer from the SP/ESP/RSP register into the BP/EBP/RBP register, and loads the SP/ESP/RSP register with the current stack-pointer value minus the value in the size operand. For nesting levels of 1 or greater, the processor pushes additional frame pointers on the stack before adjusting the stack pointer. These additional frame pointers provide the called procedure with access points to other nested frames on the stack. See “Procedure Calls for Block-Structured Languages” in Chapter 6 of the Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 1, for more information about the actions of the ENTER instruction." (Although, curiously, Free Pascal - which also implements this feature - doesn't use ENTER in assembly, last I checked.)
- bakul 8y agoThe issue is that if the local variables of A, such as X are allocated on the stack, once you return from A, the stack frame will disappear. So the example you cite can only work as long as A is active (not returned from). If A itself returns a pointer to B, at least the locals of A captured by B must be allocated on the heap and GCed once all references to a particular B disappear. Note that a number of modern languages provide full closures including Go, Haskell, JavaScript, Python, Scheme, Swift.
- Taniwha 8y agoexactly .... which kind of makes those proc pointers not as useful because you need to carefully manage lifetimes/etc yourself and failures are, well, not defined
- bakul 8y agoIt's the compiler's job to do so, not a programmers! The programmer can do whatever makes sense. For example, in Scheme (define (make-counter) (let ((n 0)) (lambda () (set! n (+ n 1)) n))) is guaranteed to work in any context where make-counter is called. Same in Go; though the syntax is a bit unwieldy due to having to specify types: func makeCounter() (func () int)) { n := 0 return func () int { n++; return n } } Google's Go compiler is smart enough to allocate only the captured variable (n in this case) for such returned functions on the heap. There should be no failures. IIRC some algol-68 compiler did remove the restriction and allowed such full closures.
- int_19h 8y agoClasses were introduced into a statically compiled programming language in Simula-67. C++ has a lot of Simula legacy, even down to some keywords (e.g. "virtual" is from there, although it meant "abstract" originally).
- pjmlp 8y agoHe did not turn to C. He had to use C at Bell Labs and C with Classes was the way to avoid it, while fitting with the existing C tooling. As after his experience with being forced to use BCPL instead of Simula, he sweared to not do it again. Plenty of interviews where he goes about it, including some of his books.