Instead of lifetimes, why not use input-output dependency?

Lifetimes in a function signature aren't just about indicating depends-on relationships — they're also plain old generic parameters and imply all the same stuff that “regular” generic parameters do. For instance, it is always the caller of a function who gets to choose the values of the generics, and the function must be valid for any choice. In this case one can lean on one’s knowledge of generic type parameters to infer something about generic lifetime parameters, but only because lifetimes are indeed generic parameters. Similarly, with HRTB it is clear that what's being expressed is “for all values of this generic parameter, such and such bound holds” which again relies on lifetimes being generic parameters. And of course, if needed you can refer to generic parameters of both kinds within the function body.

In short, since lifetimes are indeed generic parameters, we should embrace and leverage that fact, not try to hide it.

2 Likes

Thank You for all your responses. I now understand the complications in using dependency relations, especially for references inside data structures.

I hope someday lifetimes become easier.

1 Like

The issue is not with lifetimes, but with understand the complex manners in which data can depend on other data in your program. If you didn't have explicit lifetimes, you'd either need a GC to handle it all for you, or you'd have to just try your best and cross your fingers that you didn't introduce UB (or spend a lot of time in valgrind) :slight_smile:

1 Like