-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: rename lifetime
to lifespan
#487
Conversation
-1, seems like unnecessary change in terminology. "Lifespan" wouldn't make the concept any easier to grasp.... Words are just words. A newcomer would still be like "what's a lifespan?" and we'd explain it the exact same way and they'd still be confused. Also the C++ argument is weak IMO. An object's lifetime is always encoded in the source code through usage of new and delete; it's just that the compiler doesn't reason about it statically. The thing with Rust is that it can make the delete implicit since the compiler is capable of tracking the lifetime at compile time. |
@tshepang Right. Maybe just remove long?
@kemurphy The C++ argument does make a difference. It may seem subtle but is important. In C++, the termination of an object can only be determined at runtime, because the |
I don't remember seeing that usage either. |
@tshepang OK I'll leave the wording to you and others. :-) |
-1, I don't think |
-1. First Google hit for pointer lifetime is the Rust guide, followed by MSDN and StackOverflow. First page for pointer lifespan is about the dog breed. |
Yes, and so is it in Rust. When you see |
I agree lifetimes are poorly understood, but I don't accept this RFC will change the status quo in any meaningful way. For closures, the lifetime is the minimum lifetime of any object it borrows. A 'static closure cannot borrow any local references, because they have lifetime < 'static. In the following example, 'b is at least 'a, so the code is valid, but without 'b: 'a this code would not be valid because 'b could potentially be > 'a, the lifetime on the closure, and there cannot be closed over.
For traits, a lifetime is the minimum lifetime bound for the inner data of the object held in the trait's data reference. 'static traits mean that the data value held in the trait cannot have borrowed data in it. Notice how in the following example the instance of bar is moved out of the enclosing scope, but remains valid. If the lifetime of as_foo was 'static, this wouldn't work, as the lifetime of &x is < 'static.
For references, the lifetime is the duration it can be borrowed for. &'static can be borrowed forever, for &'a, it means the reference cannot live longer than 'a. For structs the lifetime is an explicit alternative to the naive ellison provided on functions like:
The guide states:
...but it seems to me it means subtly different things in different contexts. I prefer to think of lifetimes as bounds rather than spans. When you apply a lifetime to closure or trait argument, you are bounding the potential values that can be passed in that position. In the return argument you are bounding the possible return values. Using the word 'lifespan' instead of 'lifetime' is almost the archetypal case of bike shedding, but meaningfully, I think it actually proposes the wrong thing. While the term might be overloaded, a lifetime is a concrete element; it's a bound. A lifespan is not a concrete element, it's temporal range... and that's just not what a lifetime is in rust. |
@shadowmint I'm not sure if the unboxed closures are working correctly, because I got an error using the existing closure syntax. I believe the following code (playpen) is equivelant to your
I got the error:
In your second example, I believe it has nothing to do with reference lifetimes. It's the copy/move behavior for struct and box, and box is a pointer to a heap allocated memory block. When Anyway, whether it's the lifetime of references, closures, functions or structs, I believe all the lifetime thing starts with how far (or to what extent) the borrow of a reference is valid, i.e. the reference lifetime in our current terminology. If a function (or a closure etc.) takes a reference, it may contain (“store”) new references with a lifespan smaller than or equal to its input reference's. Maybe span is not the most accurate term, because if we color the lines of code where the borrow is valid, the colored lines can be disjointed. My point is it's purely static, determined by the code layout rather than the code execution sequence. (One may argue that it's related to the execution sequence, but the real relationship is both the reference lifetimes and the execution sequence are governed by the code layout.) Even the span can be disjointed, I still feel its static nature describes the concept more precisely. |
@arthurtw You can see these examples working on play.rust-lang.org. eg. http://is.gd/b0GacL Regardless of move / box semantics, the lifetime on Box<Foo + 'a> is a lifetime, and if you rename lifetime to lifespan, we will now have to refer to these as a the 'lifespan' of a trait/closure/struct, not just references. We can't just change lifetime to lifespan just for references and keep referring to them as lifetimes for closures/structs/traits. That would be completely ridiculous and confusing for everyone. The point I'm making is that lifetimes are more complicated than simply reference borrows. A summary of what lifetimes actually mean for all types would be a good first step; followed by details of how the new name is clearer in each case. I'm skeptical that a simple rename achieves anything, but I'm willing to be convinced otherwise but a thoughtful argument if one turns up. If the nothing else, if the RFC results in an update with more details in the guide, it'll still be worthwhile. |
@shadowmint Yes I know your code works with I felt several disjointed things are mingled under the big lifetime umbrella:
To me, the most important distinction is whether it's a static, compile-time thing or not. I believe using As to #3, it's a runtime thing. I don't know what's the best way to distinguish them. Maybe just avoid using the term |
After more thoughts, I agree with @shadowmint that just renaming I still feel that the confusion brought by the term
If we keep calling the valid scope of an owned/borrowed resource or a borrower pointer a “lifetime”, we shouldn’t call Is it worth submitting another RFC? Or this topic is simply too controversial to have any outcome? |
I don't think either is a better name, so I don't think anything should be changed. There's no need for churn if it's not actually providing a significant improvement. Keep in mind that people who already know Rust are going to be calling them lifetimes years into the future, so newcomers are just going to have an extra term to learn. You can't really kill the momentum on stuff like this. It's hard enough to get people to call |
Closing this PR. I agree the intent of this RFC does not justify the churn. We should however clarify/formalise how we call
The inconsistency in the rust repo itself shows a unified naming is missing. If naming it completely different (such as borrow scope) is unlikely, at least we can use "lifetime parameter" as an official term to name We need a consistent naming anyway. I will submit a short RFC for officially naming |
No description provided.