Capture Clauses as Effects
11 points by aiono
11 points by aiono
Regarding proposed syntax looking like
bar(with own(foo.a, foo.b, foo.c) || {
baz(foo.a, foo.b, foo.c);
});
How does this work here, what is foo inside of the closure? Does this syntax make it so foo itself is meaningless in the closure and only the expressions foo.a, foo.b, and foo.c work?
Personally, I don't really see the benefit of Rust introducing explicit capture clauses. The language chose implicit capture and is (maybe? likely?) adding something like move(expr) to hoist code written inside the closure outside of it to make implicit capture of clones more ergonomic.
Adding an explicit capture clause on top of that is an unnecessary second way of doing the same thing - if you prefer it, you can hoist your captures to the top of the closure and use the move(expr) there. Yes, that's admittedly only a convention and there's nothing stopping you from accidentally also capturing something in the middle of the closure - by god if we get explicit capture clauses then they must stop implicit capture from happening in the same closure!
But I still do think this is an unnecessary duplication of features that we shouldn't introduce: you may prefer capture clauses, maybe I would as well, but that's not what we have today and we cannot walk back what we already have.
you may prefer capture clauses, maybe I would as well, but that's not what we have today and we cannot walk back what we already have.
Can't it be addressed by editions? The newer Rust edition might forbid implicit captures and provide a way to automatically migrate the syntax.
I think that's possible, yes. (Or maybe?) But is it worth it / do we want to do that? Is an explicit capture clause so much better that we want to rewrite all closures over an edition? (And if it is, why'd we end up with the obviously inferior choice?)
Regarding https://blog.yoshuawuyts.com/capture-clauses-as-effects/#optimizing-for-writes I think it could just be implemented as an IDE feature, like how currently when you type .box it wraps the current expression with Box::new. In this case .use can add the expression to the own clause. That way it's ergonomic for both writing and reading.
Your comment essentially restates the linked paragraph, right? Both you and yosh describe exactly the same feature?
Have thought about this for 3 seconds, but it feels like even .use is not needed:
There's already enough information in the source to figure out what to do, you don't need to type extra .use, unlike the case for .dbg, .let and friends.
Your comment essentially restates the linked paragraph, right? Both you and yosh describe exactly the same feature?
Yes I wrote the comment without finishing the section :facepalm:
I feel like this would be less readable with many more variables no? With 3 sure it's fine, but with more I'm not entirely convinced that it is more readable.