3 ms·
I think it would be useful if the distinction did exist in Rust, so that for example, "closures that have an empty environment" were indeed referred to as lambd
by blaenk 11y ago
I think it would be useful if the distinction did exist in Rust, so that for example, "closures that have an empty environment" were indeed referred to as lambdas, and lambdas had the guarantee of being Sync, Send, Copy, Clone, etc., since they're more or less just like regular `fn` items as far as I know. As it is, a closure that captures nothing doesn't satisfy those bounds AFAIK, so their use is more restrictive than it needs to be. Instead, you're forced to write an actual `fn` definition, depriving you of the practicality and conciseness of closure syntax.
- eridius 11y agoThat sounds potentially confusing. The same syntax yields closures that sometimes are Send, Sync, Copy, Clone, etc but usually aren't? I'd rather stick with nested fn's. If you really want to be able to have inline functions that are lambdas but not closures, then maybe you should submit an RFC to add literal syntax of the form `fn(args) -> T { code }`
- blaenk 11y agoLike huon said, this is already the case, so I don't see how it could be confusing. The compiler would infer all of that and complain if the constraints are not satisfied. A literal fn syntax would be pretty cool, but I have doubts it'd ever be accepted, which is why I figured closure syntax could be adapted to allow this use case.
- dbaupp 11y agoA closure satisfies Send and Sync when it can, automatically. (I.e. with or without captures.) Satisfying Clone when the contents are Clone would be valuable for all closures, whether or not they capture anything. The most valuable special case IMO for captureless closure would be allowing them to coerce to (extern) fn types, useful for writing e.g. C callbacks directly inline.
- blaenk 11y agoYeah, I forgot about that last point, which was the original reason I got this idea.
- hermanradtke 11y agoRust figures out what parts of the environment you are using and makes a closure or lambda. The compiler figures it out for you. This is basically the Rust philosophy.
- blaenk 11y agoNot sure what you mean. I understand that the compiler figures out what to do. My point was that it might be useful to remove certain restrictions from empty-environment closures to allow them to be used in more contexts, such as wherever a regular `fn` could be used (like for FFI callbacks).