Skip to content
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

Improve explanation of lifetimes #2584

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

djmitche
Copy link
Collaborator

This approach seems to balance formalism with understanding. That is, it doesn't mention contravariance, but suggests that lifetime annotations in parameters and return values mean "opposite" things. It also leverages the understanding that types must be specified in function signatures, and are used to check types in the function body and at call sites.

@djmitche djmitche requested a review from hurryabit January 22, 2025 11:56
@djmitche djmitche force-pushed the better-lifetime-explanation branch 2 times, most recently from 7717c4f to 661774b Compare January 22, 2025 12:00
@djmitche djmitche enabled auto-merge (squash) January 22, 2025 12:00
Copy link
Collaborator

@hurryabit hurryabit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that. Some ideas for making it even better.

@@ -2,7 +2,7 @@
minutes: 5
---

# Lifetimes in Data Structures
# Struct Lifetimes
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the right title? This also works on enums. Maybe something like "Lifetimes in User Defined Types"?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, during the course I thought that using a struct with a field named document (rather than a newtype) would make the explanations somewhat more natural. What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the right title? This also works on enums. Maybe something like "Lifetimes in User Defined Types"?

I'll change it to "Lifetimes in Data Structures" which just barely fits in the sidebar.

Actually, during the course I thought that using a struct with a field named document (rather than a newtype) would make the explanations somewhat more natural. What do you think?

That's fixed in #2585.

@djmitche djmitche force-pushed the better-lifetime-explanation branch from c0e57dc to cc7cfb6 Compare January 22, 2025 17:13
@djmitche djmitche requested a review from hurryabit January 22, 2025 17:13
Comment on lines 15 to 20
Lifetimes are always inferred by the compiler: you cannot assign a lifetime
yourself. Explicit lifetime annotations create constraints where there is
ambiguity; the compiler verifies that there is a valid solution.

Lifetimes become more complicated when considering passing values to and
returning values from functions.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this removed text is load-bearing in that it counteracts a common misconception that lifetimes annotations can alter the runtime behavior of programs (in terms of how long values exist and when they are destroyed). We don't need to keep this exact phrasing but in addition to describing lifetimes as constraints we should have some explicit indication that only ownership, not lifetime annotations, control when objects are destroyed and determine the concrete lifetime of a given value.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! What do you think of the update?

Copy link
Collaborator

@fw-immunant fw-immunant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See inline comment.

This approach seems to balance formalism with understanding. That is, it
doesn't mention contravariance, but suggests that lifetime annotations
in parameters and return values mean "opposite" things. It also
leverages the understanding that types must be specified in function
signatures, and are used to check types in the function body and at call
sites.
@djmitche djmitche force-pushed the better-lifetime-explanation branch from cc7cfb6 to 96c8e13 Compare January 23, 2025 15:22
@djmitche djmitche requested a review from fw-immunant January 23, 2025 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants