Description
When a function has parameters that are of type &T
, they are given an anonymous lifetime. I propose that they should be given a lifetime named the same as the parameter, except in such cases where this shadows an already-declared lifetime.
This allows for less typing in common cases. For example,
fn foo<'a>(a: &'a [u8]) -> &'a [u8];
can be simplified to
fn foo(a: &[u8]) -> &'a [u8];
I also propose that lifetimes be allowed to use keywords as lifetime names again, so that 'self
is valid, as this allows
fn foo(&self) -> &'self uint;
My only concern with this proposal is potential confusion if a defaulted lifetime name would have shadowed a declared name (and therefore the lifetime remains anonymous). I think this can be clarified with an appropriate note on the error. If I have something like
impl<'a> Foo<'a> {
fn foo(&self, a: &[u8]) -> &'a [u8];
}
and I get a lifetime error, the compile could put a note telling me that the parameter a
has an anonymous lifetime because the lifetime 'a
was already in-scope. This note would only show up when a lifetime error occurs relating to a declared lifetime that is blocking a defaulted lifetime name.