Unboxed closures

A relatively complete description of by reference closures that aims to encompass entirety of current thinking, except for bound lifetimes.

cc @pcwalton
On Sun, Jun 15, 2014 at 02:46:19PM -0700, Gábor Lehel wrote:
> This reasoning is the opposite of the reasoning from the "Make by-reference closures the default." point from above:

Yes, I know.

> In both cases, there's tension between the assumed common case and
> the defaults elsewhere in the language. The most common case is
> assumed to be by-reference mutating closures. But everywhere else
> the language defaults to by-value and immutable. The RFC is siding
> with consistency in one case (by-value) but convenience in the other
> case (mutable). I'm not saying this is *necessarily* wrong, but I do
> think it deserves an explicit defense at least.

Noted. I'll try to add something in the next version, or perhaps make
other changes, but for posterity: my feeling is that by reference vs
by value makes less difference, since our statistics (and experience)
suggests that for most captures the two are equivalent. So for most
closures people can use by-value or by-reference as they choose, and
we'll only need to write `ref || ...` for a small percentage.

In contrast, if we have to write `|&mut: ...|` as the common case,
that's clearly going to be painful. `|&:` is much shorter and will be
written less frequently.

> If we have the generic type-level closure sugar and also infer the
> implemented trait(s) from the bodies of closure literals, then going
> for consistency on both counts is not too painful, because the
> explicit (`&mut:`) annotation can be avoided on both the type- and
> value levels.

Yes, inference would be nice, though we still must decide what to name
the types (what is `Fn`?).

In any case, I am basically convinced that inference should be
workable. Basically the idea would be to wait to decide the self type
until the end of the typeck phase (type inference). We can then
determine which (if any) traits the closure is required to support:
the self type makes no difference to typeck, only to latter passes.