Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upModify RFC #803 (type ascription) to make type ascription expressions lvalues #987
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
The history seems strange? |
This comment has been minimized.
This comment has been minimized.
|
Yeah, I screwed the history up some how, I'll try to fix it at some point |
nrc
self-assigned this
Mar 18, 2015
nrc
added some commits
Mar 18, 2015
nrc
force-pushed the
nrc:ascription-2
branch
from
9004bcd
to
18fe78e
Mar 23, 2015
This comment has been minimized.
This comment has been minimized.
|
Fixed the history and addressed @pnkfelix's comment |
This comment has been minimized.
This comment has been minimized.
|
+1 definitely an improvement. Would be nice if for the assignment case ( |
nrc
added
the
T-lang
label
May 15, 2015
This comment has been minimized.
This comment has been minimized.
|
Hear ye, hear ye. This RFC is entering the final comment period. |
nikomatsakis
added
the
final-comment-period
label
Jun 3, 2015
This comment has been minimized.
This comment has been minimized.
|
Just to clarify, this would allow writing the following? let x;
x: i32 = 10; |
nikomatsakis
reviewed
Jun 9, 2015
| The proposed solution is that type ascription expressions are lvalues. If the | ||
| type ascription expression is in reference context, then we require the ascribed | ||
| type to exactly match the type of the expression, i.e., neither subtyping nor | ||
| coercion is allowed. These reference contexts are as follows (where <expr> is a |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
reviewed
Jun 9, 2015
| taking a reference of such an expression is forbidden. I.e., type asciption is | ||
| forbidden in the following contexts (where `<expr>` is a type ascription | ||
| expression): | ||
| The proposed solution is that type ascription expressions are lvalues. If the |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Jun 9, 2015
Contributor
I find this phrasing kind of odd, though I think I agree with the intention.
It seems odd to say that <expr>: T is always an lvalue. For example, is foo(): T an lvalue? The only way I can make sense of that is if <expr>: T puts <expr> into a temporary (when it is an rvalue) and then is equivalent to referencing that temporary.
My original thought was that <expr>: T would be an lvalue if <expr> is an lvalue, and an rvalue if <expr> is an rvalue. Moreover, there would be an additional rule that when <expr>: T appears in a ref context, the type of <expr> and T must match exactly (though, strictly speaking, I think it is ok to upcast if <expr> is an rvalue).
That said, I think that the "temporary interpretation" I gave above and my interpretation behave the same way from an end-user point of view. For example, if you write &(foo(): T), and you consider foo(): T to be an rvalue, then this will create a temporary with the enclosing temporary scope and store foo() in it. But if you take the first interpretation, it will also create a temporary and store foo() in it, and presumably the lifetime of that temporary would be the same (though we would probably want to adjust the temporary lifetime rules to "see through" <expr>: T).
Anyway, just for reference, let me define my terms a bit:
- lvalue: something that can be assigned to, e.g.
x,x.f - rvalue: something that cannot be assigned to, e.g.
x() - lvalue/ref context: something where you are taking the address, e.g.
<expr>in&<expr>or in a match, etc.
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Jun 9, 2015
Contributor
Update: I realized that since lvalue controls what you can assign to, it definitely seems wrong to say that type ascription is always an lvalue, since that would permit foo(): T = 5.
I prefer just saying that it is an lvalue/rvalue based on the underlying expression, and that x: T = 5 is a reference context (and hence this annotation is basically an assertion that the type of x is T).
This comment has been minimized.
This comment has been minimized.
|
@huonw yes. |
This comment has been minimized.
This comment has been minimized.
|
@nikomatsakis updated the text |
This comment has been minimized.
This comment has been minimized.
|
It's official... the language subteam has decided to accept this RFC. |
nrc commentedMar 18, 2015
No description provided.