4 ms·
> > Nested Functions > One function may be defined inside another. A nested function may automatically "capture" non-static local variables from the enclosing
by keybored 4y ago
> > Nested Functions
> One function may be defined inside another. A nested function may automatically "capture" non-static local variables from the enclosing function (under the right conditions), allowing them to be used after the enclosing function returns.
That sounds like a closure. I expected “nested” to mean that the function is defined inside another function but doesn’t capture anything, like what you can do in Rust. (It’s IMO a “why not?” feature—why don’t more languages allow it? It’s certainly useful for helper functions which are only consumed by one other function.)
- leni536 4y agoWell, C++ has lambdas with explicit capture list. A captureless lambda is basically a function (it even converts to a function pointer), otherwise it's a closure.
- keybored 4y agoWell, what? My objection is calling a closure for “nested function”, a term I’ve never seen for such a thing.
- dragonwriter 4y agoI've seen nested function, whether or not it is also a closure, for ever; a closure is a function with an associated environment; a nested function is a function defined within another function.
- kevingadd 4y agoC# has "local functions" [1], and i feel like "nested function" is an equally good name for the concept [1] https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions https://learn.microsoft.com/en-us/dotnet/csharp/programming-...
- lmm 4y agoIt's a very natural and obvious term (far more natural than "closure" IMO). The wikipedia page has citations going back at least 20 years.
- keybored 4y agoYou win.
- shultays 4y agoEh, closure is just anested function. Just like a function can have access to global/static vars, a closure can also have access to vars in its parent scope. They are like global vars to it