[rust-dev] names needed for function types

Niko Matsakis niko at alum.mit.edu
Sun Jan 1 07:14:34 PST 2012


On 1/1/12 12:42 AM, Brian Anderson wrote:
> How did they 'meld'? Is a copying closure just assumed once you decide to copy or move the upvars?

That's how I envisioned it.  If you don't say "send", then the default 
is a boxed closure.

> Would 'fn[copy; copy a; move b]' still be allowed?

I'm indifferent on that.  I'd probably allow it just because it's easier 
to write the parser that way, but I could be persuaded to make it 
illegal fairly easily.  It would not be preserved by the pretty printer.

> It's ok. I like it better than anything we've come up with so far. I 
> know there are syntax ambiguities, but it would be more consistent if 
> we could write 'let boxed_closure = fn<copy>(x: T, y: U) ...'. That 
> could then be potentially extended in an obvious way to named 
> functions as well.

Hmm, I hadn't considered "<" and ">".  They look nice; in fact, I rather 
prefer them to "[" and "]" because they stand out more from the argument 
list.  It need not be ambiguous to the parser as `copy`, `move`, and 
`send` are keywords (or could be), but it is somewhat ambiguous to the 
human eye.

I think either syntax could easily be extended to (nested) named 
functions.  Using "<" and ">" makes it more awkward to combine with 
generics.  Of course, currently our implementation assumes that 
functions either capture their environment or define generic parameters, 
but not both.  I am not sure if that is a restriction we will always 
want, however, although I suspect that you rarely need both in practice.

Here is how it would look with square brackets:

     fn foo() {
         // Without generics:
         fn no_env(a: A, b: B) { ... }
         fn boxed_env[copy](a: A, b: B) { ... }
         fn boxed_env[copy a; move b](a: A, b: B) { ... }
         fn unique_env[send; copy a; move b](a: A, b: B) { ... }

         // With generics:
         fn no_env<A, B>(a: A, b: B) { ... }
         fn boxed_env[copy]<A, B>(a: A, b: B) { ... }
         fn boxed_env[copy a; move b]<A, B>(a: A, b: B) { ... }
         fn unique_env[send; copy a; move b]<A,B>(a: A, b: B) { ... }
     }

and with curly braces:

     fn foo() {
         // Without generics:
         fn no_env(a: A, b: B) { ... }
         fn boxed_env<copy>(a: A, b: B) { ... }
         fn boxed_env<copy a; move b>(a: A, b: B) { ... }
         fn unique_env<send; copy a; move b>(a: A, b: B) { ... }

         // With generics:
         fn no_env<A, B>(a: A, b: B) { ... }
         fn boxed_env<copy; A, B>(a: A, b: B) { ... }
         fn boxed_env<copy a; move b; A, B>(a: A, b: B) { ... }
         fn unique_env<send; copy a; move b; A, B>(a: A, b: B) { ... }
     }


Niko


More information about the Rust-dev mailing list