logoalt Hacker News

jcranmeryesterday at 12:15 PM2 repliesview on HN

When you want to use lambdas, but your language doesn't have lambdas, so you reach for the nearest thing instead.


Replies

ueckeryesterday at 1:12 PM

Lambdas are just anonymous nested functions. But I like named nested functions more because they are more readable and would prefer them in most cases. Ideally you have both as most languages have.

I always wondered why C++ only added lambdas, but observing WG21 for a while, I assume this is just a random walk in language design. (not that it is different in WG14)

show 4 replies
mananaysiempreyesterday at 12:33 PM

C++ bundles together a way to write functions inline in an expression (what I’d call “lambdas” in general) and a way to create closures with strictly nested lifetimes, but there’s no law of nature tying the two together. Even in C++ the essentially separate declaration “auto f = [&](... blah ...) { ... 50 lines of code ... };” is pretty common. (And of course GCC’s nested functions predate C++11 by twenty years.)