On 10/06/2012 11:30 AM, Patrick Walton wrote:

I like this. The only concern, as a comment pointed out, is that "*"
might be slightly confusing; maybe "ref" is better.

I like it too. Though I wonder if the ambiguity between &-as-a-reference-taker and &-as-a-pattern is actually problematic. Consider two cases (assuming we use & here):

#1:

   let foo = {1,2};
   let {&a, &b} = foo;

#2:

   let x = 1;
   let y = 2;
   let foo = {&x, &y};
   let {&a, &b} = foo;

It seems to me that in both cases you're introducing two variables, a and b, of type &int. In #1 they point into foo, using the & to "take references" to the record components; in #2 they point to x and y respectively, using the & to "match against" the existing &-types inside the record.

I think to a user these are "intuitively" compatible interpretations, and supporting both depending on the type being matched against would not lead to any deep surprises; one could always force the "take a reference" form for #2 by writing it with a double-ampersand (something the user might even guess to do):

   let x = 1;
   let y = 2;
   let foo = {&x, &y};
   let {&&a, &&b} = foo;

Here a and b are references-to-references, type &&int. Which is, conveniently, just how &&int reads anyway. They could also force the _opposite_ interpretation (that is, "deref the & values") using a type annotation (at least in let-patterns):

   let x = 1;
   let y = 2;
   let foo = {&x, &y};
   let {a, b} : {int,int} = foo;

The only practical (as in "maybe hard to implement") difference is that the &-symbol _would_ have a syntactically ambiguous denotation in patterns, that would be resolved one way or another during typechecking. But patterns often have type-indeterminate parts they need to pick up from the other variables at play in inference, and it seems to me a plausible special-case in the unification rule on &-patterns, and this might work ok.

Maybe I'm being naive though. I don't really know that code. Comments from anyone who does?

-Graydon
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to