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 upType ascription (tracking issue for RFC 803) #23416
Comments
steveklabnik
added
the
B-RFC-approved
label
Mar 18, 2015
theemathas
referenced this issue
Apr 1, 2015
Closed
RFC: Add syntaxic sugar for struct expressions fields from variables of the same name #306
alexcrichton
added
the
T-lang
label
Aug 11, 2015
bors
added a commit
that referenced
this issue
Dec 19, 2015
This comment has been minimized.
This comment has been minimized.
|
There's one thing making type ascription less convenient than it could potentially be. While it has the same operator priority as Compare these two expressions
I'm not sure if it is worth fixing or if it can be fixed without hacks, but it is worth discussing at least. |
This comment has been minimized.
This comment has been minimized.
|
BTW: This has is a breaking change for crates that implement a similar macro syntax already, such as prettytables. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
cc @nrc |
alexcrichton
added
B-unstable
and removed
B-RFC-approved
labels
Feb 18, 2016
petrochenkov
referenced this issue
Mar 11, 2016
Closed
Revise type ascription operator to use type equality, not coercion #1539
This comment has been minimized.
This comment has been minimized.
|
Is it a good idea to only do ascription inside parentheses, so |
petrochenkov
referenced this issue
May 3, 2016
Closed
syntax: Parse field/method access after type ascription and cast expressions #33380
This comment has been minimized.
This comment has been minimized.
|
For the record: a patch implementing support for method chaining with type ascription #33380 |
This comment has been minimized.
This comment has been minimized.
ottworks
commented
Jul 3, 2016
•
|
Why is this in the book if it won't compile? (1.9.0) |
This comment has been minimized.
This comment has been minimized.
|
@0TT what exactly? |
This comment has been minimized.
This comment has been minimized.
|
@0TT Type ascription is specifically when adding a |
This comment has been minimized.
This comment has been minimized.
ottworks
commented
Jul 3, 2016
|
Oops. I accidentally typo'd a semicolon as a colon:
|
This comment has been minimized.
This comment has been minimized.
|
that happens, how about an RFC to replace all semicolons with a less confusable character? let plus_two = |x| {
let mut result: i32 = x✓
result += 1✓
result += 1✓
result
}✓
assert_eq!(4, plus_two(2))✓makes the language even look safer, everything is checked after all |
This comment has been minimized.
This comment has been minimized.
ottworks
commented
Jul 3, 2016
|
Should use emoji tbh. A language for the future. |
This comment has been minimized.
This comment has been minimized.
|
re the soundness question in the OP, this comment is probably the best summary. |
nrc
added
B-RFC-approved
B-RFC-implemented
labels
Aug 29, 2016
This comment has been minimized.
This comment has been minimized.
|
Before stabilizing this feature, I would like to see how this potentially impacts optional and named arguments, namely the Could named arguments use this syntax if it wasn't for type ascription? If yes, and no other syntaxes could be devised that are as optimal as I believe named arguments would be much more widely used than type ascription, and the syntax optimization for ergonomics should go there instead. |
This comment has been minimized.
This comment has been minimized.
|
Lossy conversion without an explicitly stated target type sounds like a huge footgun to me. TBH I don't see why |
Mark-Simulacrum
added
C-tracking-issue
and removed
C-enhancement
C-feature-request
labels
Jul 22, 2017
This comment has been minimized.
This comment has been minimized.
omarabid
commented
Aug 12, 2017
|
No progress on this one? |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
SoniEx2
commented
Mar 30, 2018
|
hey can we remove type ascription in favor of an std::identity function? std::identity::<Type>(stuff)current alternatives include:
if we had it in std, we wouldn't need to introduce new syntax, and we wouldn't need to deal with the soundness issues and stuff, and it could be freely used in macros. |
This comment has been minimized.
This comment has been minimized.
|
Both a function or a closure cause a value to be moved, which affects its life time more than just a type annotation would. |
This comment has been minimized.
This comment has been minimized.
SoniEx2
commented
Mar 30, 2018
|
|
This comment has been minimized.
This comment has been minimized.
|
Or |
This comment has been minimized.
This comment has been minimized.
|
The problem is that calling a function takes argument's ownerwhip. This cannot be cancelled by being an intrinsic (that is magical but stil has to appear to be a function), right? let s = "foo".to_string();
(s : String).len();
println!("{}", s); // OK
identity::<String>(s).len();
println!("{}", s); // ERROR use of moved value: `s` |
This comment has been minimized.
This comment has been minimized.
SoniEx2
commented
Mar 31, 2018
|
It is meant for use without bindings. If you wanna use it with bindings, just use |
This comment has been minimized.
This comment has been minimized.
|
Oh, of course. So it may be temporary lifetime things (for |
This comment has been minimized.
This comment has been minimized.
SoniEx2
commented
Mar 31, 2018
•
|
Won't NLLs solve that? |
This comment has been minimized.
This comment has been minimized.
|
How about this as a more specific plan:
|
scottmcm
referenced this issue
Apr 5, 2018
Merged
RFC: Reserve `try` for `try { .. }` block expressions #2388
This comment has been minimized.
This comment has been minimized.
|
I think @scottmcm's plan sounds good. Repeating what I said on #rust-lang: I can live with the problem of not having 100% consistency personally. Some inconsistency is to me better than not having the feature at all. Unfortunately, with the current language and our backwards compatibility promises, we can't achieve 100% consistency with typing judgements in the language across the board and I'm not advocating that we write: let foo as type = expr;That said, I think the idea due to @cramertj to have some sort of An example: let vec = iter.map(..).filter(..).collect().at!(Vec<u32>);The benefit of post macros here is that it allows chaining... however, I think it is unlikely to have many type ascriptions per expression. |
zackmdavis
added a commit
to zackmdavis/rust
that referenced
this issue
Jun 24, 2018
zackmdavis
added a commit
to zackmdavis/rust
that referenced
this issue
Jun 24, 2018
zackmdavis
added a commit
to zackmdavis/rust
that referenced
this issue
Jun 24, 2018
scottmcm
referenced this issue
Jun 26, 2018
Open
Conversions: `FromLossy` and `TryFromLossy` traits #2484
zackmdavis
referenced this issue
Jul 2, 2018
Closed
Error message provides suggested fix that does not compile #51972
This comment has been minimized.
This comment has been minimized.
Ichoran
commented
Jul 12, 2018
|
As a long-time user of Scala, I have only very rarely seen anyone confuse (Numeric types in Scala have a notion of "weak conformance" which means they can get coerced unexpectedly; this has turned into a a fruitful source of puzzlers. Rust has much less of an issue presently, but dropping the distinction between pure ascription and request for coercion is likely to erode the improvement. It would be especially pernicious if the very thing you would use to check your assumptions could break them instead!) |
Centril
referenced this issue
Sep 23, 2018
Open
Strange behavior decl_macro + try_blocks + type_ascription #54455
This comment has been minimized.
This comment has been minimized.
mikhail-krainik
commented
Mar 19, 2019
•
|
@nikomatsakis could we improve error message in this case
I got
According this RFC type ascription should contain |
This comment has been minimized.
This comment has been minimized.
|
cc @estebank ^-- We discussed this at the all hands on a night out... the idea, if you remember, was that you check for this pattern when the LHS of |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@estebank Oh haha; I even reviewed that one... |
nikomatsakis commentedMar 16, 2015
Tracking issue for rust-lang/rfcs#803. Important points:
expr:Typesyntaxrefpositions:let ref x = <expr>/let mut ref = <expr>match <expr>: Type { ref x => ... }(<expr>: Type).method_with_ref_self()&[1, 2, 3]: &[u8]