Can we talk about C++ style lambda captures?

167 points BoltActionPiano 50 comments

With all this back and forth on ergonomic clones into closures, it seems there's a huge tension between explicit and implicit.

  • Adding a trait means bulking up the language with a bunch of "is this type going to magically behave in this way in closures" traits. We've improved on the "what types should have it?" question a lot, but it's still a bit magic.
  • If we're going to add syntax, and people are debating on the ergonomics and stuff... like.. C++ did this, and honestly it's great, and explicit, which leads me to...

If there's unresolvable tension between explicit and implicit for ergonomics, then the only option is to make the explicit ergonomic - and C++ did this.

I know the syntax probably doesn't work for Rust, and I don't really have much of a proposal now, but just like... You can capture by copying, and capture by borrowing, you can specify a default, and also override it per variable.

Why not like:

clone || {
    // all values are cloned by default
}

move (a, b), clone (c), borrow (d) || {
    // a and b are moved, c is cloned, d is borrowed
}

clone, move (a, b) || {
    // a and b are moved, rest are cloned
}