5 ms·
> Particularly, if we could assume that nested functions automatically and properly capture the arguments to the parent function. well, how would you do that ?
by jcelerier 6y ago
> Particularly, if we could assume that nested functions automatically and properly capture the arguments to the parent function.
well, how would you do that ?
- doteka 6y agoI don’t know any other language with lambdas which requires me to spell out explicitly which variables it should close over... so you tell me how Java, Scala, Clojure, Kotlin, Python, JavaScript, Rust, Go, C# and tens of others managed this clearly impossible feat of engineering.
- gpderetta 6y agoForced heap allocation and GC of course that would be unacceptable in C++. IIRC Rust uses capture modifiers similar to C++.
- steveklabnik 6y agoRust is not really the same as C++ here, you get "the compiler infers how to capture things" as the default behavior, and "take everything by value", which happens with the "move" keyword. We've found that these two cases cover the vast, vast, vast majority of usage, and so C++-style specific capture modifiers aren't necessary. You can emulate C++ style stuff by taking references outside of the closure and using move to capture only the reference.
- DCoder 6y ago> I don’t know any other language with lambdas which requires me to spell out explicitly which variables it should close over PHP closures have to specify which variables they close over, and whether it's done by-val or by-ref [0]. Nowadays there's also a shorthand single-expression-closure syntax that closes implicitly and by value [1]. [0]: https://www.php.net/manual/en/functions.anonymous.php#example-167 https://www.php.net/manual/en/functions.anonymous.php#exampl... [1]: https://www.php.net/manual/en/functions.arrow.php https://www.php.net/manual/en/functions.arrow.php
- TeMPOraL 6y agoBy having the compiler generate the equivalent of [&args...], where args are names of arguments in the parent function's header.
- jcelerier 6y agowhy [&args...] and not [args...] ? do you really want to capture by reference when you're setting a lambda as a callback ? (likewise in the other direction - tons of arguments for every default, already debated ad nauseam)
- gpderetta 6y agoAnd why specifically args instead of all locals?
- nwallin 6y agoYou mean like this? https://godbolt.org/z/jvG3hY https://godbolt.org/z/jvG3hY