On 05/15/2014 09:30 PM, Tommi wrote:
On 2014-05-16, at 7:14, Daniel Micay <[email protected]> wrote:
On 16/05/14 12:10 AM, Tommi wrote:
I was just wondering, why do we have to explicitly specify the lifetimes of
references returned from functions? Couldn't the compiler figure those
lifetimes out by itself by analyzing the code in the function?
Type inference is local to functions, so it couldn't do that. It would
be impossible to have libraries if inference needed to be global across
the entire program, and the errors would be *signicantly* harder to
understand as they would span across countless functions.
I'm sorry, but for some reason I completely fail to understand your explanation.
My thinking of how the compiler would work is that whenever it compiles a function, it
would analyze it and in some sense "write in" the named lifetimes for that
function. The named lifetimes would still be explicitly part of the signature of the
function in the compiler's internal representation of the function, but not be visible in
the source code.
I believe the compiler does this anylisis when the lifetimes in a
signature are wrong - but just for the error message. It tries to
suggest lifetimes that will work, but the analysis is not foolproof and
can't be done automatically generally (idk why, ask niko).
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev