Actually this makes more sense, due to the following analogy:

    // given this:
    let x = ~"foo";
    let v;

    // ensures that `v` is of type `&str`
    v: &str = s;
    // ensures that `v` is of type `&str` by declaring it
    let w: &str = s;

Patrick Walton <pwal...@mozilla.com> wrote:
>
> On 3/23/13 10:52 PM, Chris Peterson wrote:
>> To reduce the number of rust keywords, I offer the lowly colon as
>> alternative syntax for the `as` keyword.
>
> 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:
>
>      trait Number {
>          fn make_number() -> Self;
>          fn incr(self) -> Self;
>      }
>
>      impl Number for int {
>          fn make_number() -> int { 1 }
>          fn incr(self) -> int { self + 1 }
>      }
>
>      impl Number for uint {
>          fn make_number() -> uint { 2 }
>          fn incr(self) -> uint { self + 1 }
>      }
>
>      fn main() {
>          println(Number::make_number().incr().to_str());
>      }
>
> This results in an error because the typechecker can't tell whether 2 or
> 3 should be printed. But type ascription would solve it:
>
>      fn main() {
>          println(Number::make_number():int.incr().to_str()); // 2
>      }
>
> However, a cast to int with `as` would convert both uint and int to int
> and therefore it wouldn't help the typechecker enough.
>
> Patrick

-- 
-- Kang Seonghoon | Software Engineer, iPlateia Inc. | http://mearie.org/
-- Opinions expressed in this email do not necessarily represent the
views of my employer.
--
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to