Hacker News new | ask | show | jobs
by steveklabnik 2440 days ago
Yeah I mean, it really depends on the exact type system features and implementation, but that sounds like a different error than the kind I'm talking about. For example, consider this code, imagining Rust had type signature inference:

    fn foo() {
        String::new()
    }
    
    fn bar() {
        foo()
    }
    
    fn main() {
        let s: String = bar();
    }
Imagine you modify foo, and now you're accidentally returning a Vec, not a String:

    fn foo() {
        Vec::new()
    }
    
    fn bar() {
        foo()
    }
    
    fn main() {
        let s: String = bar();
    }
You'd get an error like this:

    error[E0308]: mismatched types --> src/main.rs:10:21
       |
    10 |     let s: String = bar();
       |                     ^^^^^ expected struct `std::string::String`, found struct `std::vec::Vec`
       |
       = note: expected type `std::string::String`
                  found type `std::vec::Vec<i32>`
You changed foo, but the error points to bar, in main. None of this is related to the code you changed. But in Rust today, you'd get

    error[E0308]: mismatched types
     --> src/main.rs:2:9
      |
    1 |     fn foo() -> String {
      |                 ------ expected `std::string::String` because of return type
    2 |         Vec::new()
      |         ^^^^^^^^^^ expected struct `std::string::String`, found struct `std::vec::Vec`
      |
      = note: expected type `std::string::String`
                 found type `std::vec::Vec<_>`
This points right to the issue: you're returning a vec, and not a string, in foo.

There are other issues too; global inference and subtyping have problems. While Rust doesn't have subtyping generally, we do in lifetimes...

Beyond all of that though:

> it removes a lot of noise and redundant information while largely leaving the "intent" of the code in place.

Rust's perspective on this is that the type signature is what communicates your intent. It's like a unit test. You write down what you expect, and then the compiler's job is to check that your code does what you said you were going to do. This seems like philosophically at odds with what you expect, which is fine of course, but is probably where a lot of the divergence comes from.

1 comments

Interesting. Thank you for taking the time to elucidate the issue so clearly.
Any time!