-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Revise type ascription operator to use type equality, not coercion #1539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Revise type ascription operator to use type equality, not coercion #1539
Conversation
coercing. This is the semantics that wound up being implemented and, on reflection, seems to represent a simpler and perhaps better choice.
I wanted to use, e.g. I have grown to like |
I'd prefer Off-topic:
|
True, though there is a kind of symmetry -- in both cases, the |
I was quite persuaded by the original RFC's logic that type ascription should behave analogously to the existing positions in the language where we can specify a desired type, like function arguments, returns, and |
I agree with @glaebhoerl, the very idea of type ascription is the ability to assert that a value having a given type, hinting the compiler. Without this, type ascription has very few usecases. |
With respect to the issues with lvalues vs. rvalues --- I'm not confident I fully understand the details, but wouldn't it be possible to resolve it such that a type-ascribed expression is demoted from an lvalue to an rvalue if and only if a coercion is actually triggerred? It makes intuitive sense to me that a coercion is a bit of runtime computation implicitly inserted by the compiler, which results in a temporary. (I.e., if I had written out by hand what the compiler inserted for me, it would have similarly created a temporary.) And it seems like this would rule out the problematic case which has to do with coercions happening in lvalue contexts. Or does this end up not flying because of things like deref coercions (which seem rather lvalue-y)? |
Coercion was really the main motivation for having type ascription for me - I feel like asserting that a value has a type (without coercion) is not valuable enough to warrant it's own syntax (it could be a magic macro or something). I think it is nice to be able to coerce with ascription, especially around DST type, e.g., where
doesn't type check, today you need to introduce two local variables just to make the coercion: There are also problems with macros, e.g. you have to use |
On Sun, Mar 13, 2016 at 04:02:25PM -0700, Nick Cameron wrote:
Well, I'm not sure I agree. To me, the main use case for type
Today one can write
Hmm. I don't typically hit this, since usually it involves a coercion |
On Sun, Mar 13, 2016 at 05:17:57AM -0700, Gábor Lehel wrote:
Yeah, I think this is the strongest argument against the change. |
On Sun, Mar 13, 2016 at 01:15:53PM -0700, Gábor Lehel wrote:
It is possible but highly undesirable, in my opinion. Making Example: let value: Box<Type> = Box::new(some_type);
let ref mut x = value: Box<Trait>; Now UPDATE: s/impossible/possible/ in first sentence :) |
It's definitely been the plan of record that I continue to wish that there were a simpler way of satisfying these needs than having two very similar mechanisms (
There's a subtext here I wanted to clarify: do you think the feature would pull its weight if it included coercions? |
I don't know. Small improvements like coercions, better parsing, make the feature a bit more usable and tip the scales in favour of keeping it. |
If we should follow the logical evolution of the language, maybe it's how we are calling collect that ought to change. For many other generic-interfaces I have come to prefer explicit types like you demonstrate: Just a thought from the perspective that adding a new operator just to solve .collect() or .into() ergonomics is maybe the wrong direction. |
I think I decided to just withdraw this RFC. In general, I'm of mixed minds about the ascription operator. @bluss makes an interesting point. Nonetheless, if we are to have it, I think I agree it should act analogously with |
Revise type ascription operator to use type equality instead of coercing. This is the semantics that wound up being implemented and, on reflection, seems to represent a simpler and perhaps better choice.
I sort of like the idea of being able to write out what type an expression has without also triggering coercions. And most of the implicit coercions we have have explicit forms (e.g.,
&foo[..]
, for the example in the RFC,foo as &Trait
for trait objects), so the:
operator is not strictly needed there (though I am reluctant to encourage the use ofas
). (In any case, for the trait case, we may be able to improve inference in the compiler as well.)I'm curious to get people's thoughts on this! Haven't fully made up my mind myself, though obviously I'm sympathetic since I took the trouble to write up the amendment. :)
cc @eddyb @petrochenkov @rust-lang/lang