On 2014.03.29 13:53:49 +0100, Zoltán Tóth wrote:
> Devs, please explain how such option could decrease the safety of the
> language. As it would be just that, an option, an opt-in one.

As somebody else said before, rust's indexing operator is like C++'s
std::vector::at() which throws an exception in case of an invalid index
that may be caugth to provide proper error handling. In C++ the switch
that is being proposed would kill the exception and would therefore also
render the error handler useless.

In rust, consider something like this:

    use std::io::{BufferedReader, stdin};
    use std::from_str::FromStr;

    static values: [int, ..5] = [1,2,3,4,5];

    fn main() {
        let mut stdin = BufferedReader::new(stdin());

        println!("Enter an index: ");
        for line in stdin.lines() {
            match line {
                Ok(l) => {
                    spawn(proc() {
                        let idx: int = FromStr::from_str(l.trim()).expect("Not 
a number");
                        println!("The value at {} is {}", idx, values[idx]);
                    })
                }
                _ => {}
            }
            println!("Enter another index: ");
        }
    }

The task spawned for each entered number provides a boundary in case of
a failure. So if the user enters an invalid index (or something that
can't be interpreted as an int) only the task dies, prints its error
message and the outer loop just continues.

Removing the boundary checks would change the API just like removing the
exception from the C++ code. Suddenly you don't have the (rather
simplistic) error handling that just kills the task anymore, but can end
up with sefaults or memory corruption (if this was writing to memory).

The proposed switch would therefore possibly turn previously _handled_
errors into unhandled ones. I think that's (at least in part) what
Daniel meant when he said that you'd effectively create a new language
dialect.

Björn
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to