13 ms·
The amusing thing is that C++ still doesn't have nested functions. (Although I think that was a GCC feature at one point.) As I point out now and then, there a
by Animats 6y ago
The amusing thing is that C++ still doesn't have nested functions. (Although I think that was a GCC feature at one point.)
As I point out now and then, there are two basic concepts here - closures and anonymous functions. A lambda is both. All four combinations (functions with/without closure, and with/without names) are possible. There's a tendency to confuse the two. An anonymous function is syntactical sugar. A closure is a run-time construct.
- Python and Javascript have nested functions with inheritance, which are closures. Most garbage collected languages have that, because it's easy to implement. Python also has lambdas, which von Rossum didn't want to put in but did to shut up the crowd that wanted them.
- Rust has nested functions without inheritance. A nested function can't access its outer scope. Rust also has lambdas, and they can inherit. C++ has now gone the same route. Non-GC languages need compile-time work to implement a closure, since something has to make a copy of what's inherited and worry about its lifetime.
- It's possible to have anonymous functions without inheritance. The classic example is a sort key function, where a sort function is called with a function that defines the test.
- MiroF 6y agobut not all lambdas are closures
- pak9rabid 6y agoI thought the difference is more in how the runtime is structured, i.e. a closure has the ability to make use of the scope in which it's defined in, so that it can make use of variables and functions within it when it's actually called.
- MiroF 6y agoI don't understand, sorry. By your definition, not all lambdas are closures.
- nwallin 6y agoLambdas are (among other things) nested functions. The "tendency to confuse the two" is a feature, not a bug. Bob knows how to draw quadrilaterals. He uses this skill to draw rhombuses, rectangles, and parallelograms. IMHO you're arguing that Bob doesn't know how to draw rhombuses because the method he uses to draw rhombuses is a method to draw parallelograms. Am I misunderstanding you?
- ben509 6y agoI think there is a distinction. A nested procedure ("function" is a bit misleading in imperative languages) can close over variables in the outer scope, but has to have its own isolated control. A truly[2] anonymous procedure can also close over control flow: fun hasZeros(ints: List<Int>): Boolean { ints.forEach { if (it == 0) return true // returns from hasZeros } return false } That's an example from Kotlin's docs. [1] [1]: https://kotlinlang.org/docs/reference/inline-functions.html https://kotlinlang.org/docs/reference/inline-functions.html [2]: "truly" in the sense it's never assigned to a name, and is known to not escape its surrounding scope, which is necessary if the compiler is going to allow it to, e.g. do a non-local return.
- nwallin 6y agoI think I'm glad C++ doesn't have that feature. Can you provide a ... slightly more real world example of what non-local returns are for? As described it sounds equally capable of shooting both varmints and one's own feet.
- comex 6y agoIn C++, if you leave the capture specifier at the beginning of a lambda expression empty (e.g. [](int x) { return x + 1; }), you can't capture variables from outer scopes, so you effectively have an anonymous function without inheritance. Non-capturing lambdas can be coerced to regular function pointers, so they behave almost identically to true functions, though they technically have different types (before being coerced). Rust also allows the "non-capturing lambda to function pointer" conversion but (for better or worse) doesn't have explicit syntax for non-capturing lambdas; a lambda automatically qualifies if it doesn't mention any outer variable names.
- inetknght 6y ago> A lambda is both. bool foo() { return [](){return false;}(); } No capture here; no closure. Adding `-O2` to GCC inlines the lambda. > The amusing thing is that C++ still doesn't have nested functions. Also amusingly, it does have anonymous types which can have function members. bool bar() { struct { bool orly() { return false; } } a{}; return a.orly(); } It's fun to send this type of stuff to code review. Edit: Oh and I forgot; you can refine the member to be static so you don't need to instantiate it first. bool bar() { struct a { static bool orly() { return false; } }; return a::orly(); } [0] https://gcc.godbolt.org/z/Pjdnre https://gcc.godbolt.org/z/Pjdnre [1] https://gcc.godbolt.org/z/hc73GP https://gcc.godbolt.org/z/hc73GP
- WalterBright 6y agoD has nested functions that can access the enclosing function's variables (and it's enclosing function's variables, on up the call stack!). It's a feature I first encountered with Pascal. It's simple to implement. After using them extensively for years now, they're a big improvement.
- ben509 6y agoBoth Pascal and C borrow heavily from ALGOL, which both languages tried to simplify by leaving out various features.
- quantified 6y agoPart of why I miss programming in Pascal, which was only in school. Not enough minutes in the week to get to enjoy D.
- ridiculous_fish 6y agoAre D's nested functions truly functions - for example are they ABI-compatible with a top-level function? If so, how do they get access to the stack frame? gcc has this feature, it is implemented by constructing a trampoline-style function on the stack. This requires an executable stack, so isn't really a thing any more.
- moonchild 6y agoD has native support for 'delegates'; closures, essentially. Function literals that reference objects from an outer scope are delegates, and ABI-compatible with other delegates. Regular function pointers can trivially be converted to delegates, so if you want to store function pointers, you should store delegates instead and convert transparently as needed. You can't pass delegates into C functions (because they're only in the D abi, not the C abi), though most C libraries that take a callback also take a 'state' pointer that you can use instead. Tl;dr: it's a bit complicated, especially wrt communication with c libraries, but in general they're interchangeable with other functions.
- WalterBright 6y ago
- pjmlp 6y agoAda, Pascal and C# have nested functions support. In C#, I never used them, rather reach out for lambdas. In Ada and Pascal, I never used them beyond book exercises. Never saw the use beyond cluttering function code.