News for hackers. Stuff that matters.
Re:Rust is mostly safety by iopq on Thursday December 29, 2016 @11:58AM (#13278375)
No, because if you explicitly converted to another type, that's what you wanted.
Re:Rust is mostly safety by zzzcpan on Thursday December 29, 2016 @12:35PM (#13278635)
Sometimes you want it to be lossy, but not most of the time, and yet there is no choice. I had a bug caused by that, that's why I remember it. Silent explicit type conversions are essentially unsafe.
Re:Rust is mostly safety by eridius on Thursday December 29, 2016 @01:40PM (#13279092)
What do you mean by "silent explicit type conversion"? If you said "silent type conversion" I'd read that as "implicit type conversion". But you said "explicit", which means you've got it very clearly in your code that you're doing a type conversion (to a smaller type), so what's silent about that?
Re:Rust is mostly safety by zzzcpan on Thursday December 29, 2016 @02:08PM (#13279267)
Silent in terms of compiler not complaining.
Re:Rust is mostly safety by eridius on Thursday December 29, 2016 @02:58PM (#13279627)
Why would the compiler complain? Doing a narrowing type conversion is a perfectly legitimate thing to do. So when you ask the compiler to do it, it should.
Re:Rust is mostly safety by kbp on Thursday December 29, 2016 @03:55PM (#13280056)
It forces you to explicitly say that you know you're forcing a value into a narrower type; I think the fact that might mean loss of information is understood, by definition. What would you like it to do?
Re:Rust is mostly safety by zzzcpan on Thursday December 29, 2016 @04:20PM (#13280221)
I would like it to tell me if I accidentally converted to a narrower type. This is a problem, because I don't necessary see the type I'm converting from due to type inference or simply am too far from the context where that type is declared and have to make assumptions to keep going. These assumptions of course fail sometimes and cause bugs. Same problem with precisions, by the way. I'm not sure how exactly compilers should fix this, the easiest fix seems to simply have different operators to explicitly allow lossy conversions, when necessary. But the bigger deal would be to treat numbers as sets of possible values with solvers or whatever to warn about mistakes where you use unhandled values in the code somewhere.
Re:Rust is mostly safety by iopq on Friday December 30, 2016 @06:15PM (#13288351)
Rust actually has this, this works:

    fn main() {
        let x = 5i32;
        let y: i64 = x.into();
        println!("{}", y);
    }
this doesn't work:

    fn main() {
        let x = 5i32;
        let y: i16 = x.into();
        println!("{}", y);
    }
you have to write:

    fn main() {
        let x = 5i32;
        let y = x as i16;
        println!("{}", y);
    }
where `as` has the potential of being lossy!
Re:Rust is mostly safety by icebraining on Friday December 30, 2016 @06:53PM (#13288528)
I agree that this is where we should be headed; it seems to me that Liquid Haskell, which was submitted to HN recently[1], could actually do what you need, since it uses an SMT solver to check for preconditions.

The casting function could specify the valid input values, and force the programmer to handle the rest of the cases when the input type is wider.

[1] https://news.ycombinator.com/item?id=13125328

Re:Rust is mostly safety by PhineasRex on Thursday December 29, 2016 @02:23PM (#13279360)
> Silent explicit type conversions are essentially unsafe.

This is a contradiction, something cannot be both silent and explicit.

Re:Rust is mostly safety by ldev on Thursday December 29, 2016 @01:29PM (#13279000)
Sounds like an implicit conversion.
Re:Rust is mostly safety by crossroads1112 on Thursday December 29, 2016 @01:38PM (#13279082)
In Rust, lossy conversions only occur if you you explicitly write `var as type` and even that syntax is limited to certain types e.g. you can't coerce an integer to a function. In order to do something crazy like that, you'd need to call the unsafe `mem::transmute` function. The language cannot be much safer in this regard short of disallowing any sort of type conversions.