3 ms·
The trouble is, when you say it's OK to infer "from the current code" that includes your function call, which is the thing where you're surprised it happens. T
by tialaramex 5y ago
The trouble is, when you say it's OK to infer "from the current code" that includes your function call, which is the thing where you're surprised it happens.
There's no coherent reason why it'd be OK for Rust to conclude that x is a u64 from
let x = some_u64.add(6); // the u64 type implement Add returning a u64
and yet not OK for Rust to conclude that x is a u64 from
let x = someFunction(); // someFunction was defined to return u64
You had to explicitly change the return type of the method to cause yourself a surprise, Rust won't chase this rabbit through a warren, every individual function has to declare the types of its parameters and returns, so if you'd changed the function body not its declared return type, it could not have caused any change in type inference in other functions.
- ayende 5y agoThat isn't the problem. This is the issue: let x = str.parse()?; someFunc(x); The function called here is dependent on what `someFunc` will accept. That means that I can change what values will be accepted here by changing a completely different piece of code.
- ReleaseCandidat 5y agoOnly if you think of other code in the same function as 'a completely different piece of code'. With global type inference, the whole program is inferred for example.