[rust-dev] Proposal to replace the `as` keyword with syntax

Chris Peterson cpeterson at mozilla.com
Mon Mar 25 16:58:45 PDT 2013


On 3/24/13 1:09 AM, Patrick Walton wrote:
> I'd like to use the colon for type ascription (i.e. an *assertion* 
> that a value has some type), which is different from a cast. This 
> would be useful to help along the typechecker in cases in which it 
> doesn't have enough information to determine the result of an 
> expression. For example: 
I see that Scala uses the colon for type ascription like you describe, 
so you have precedent on your side. :)

http://docs.scala-lang.org/style/types.html#ascription

Similar syntax is OCaml's :> for (static) upcasts and :?> for (dynamic) 
downcasts. That's not helpful for casting types that don't have a 
subtype relationship, such as u8 and u16 in my example earlier. However, 
rust's integral types could be special-cased such that:

    let big = 0x54321;
    let a = big :> u16; // big & 0xffff truncated like C
    let b = big :?> u16; // fail! runtime error because big is out of range


chris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20130325/5cb9ecd1/attachment.html>


More information about the Rust-dev mailing list