[Pitch] Borrowing Accessors

borrowing get is just a get accessor where the self value is borrowed for the duration of the getter function call. After the call completes, the returned value is effectively a copy of the property, just as with any other get access.

A borrow accessor returns a borrow of the accessed value, which is not a copy and requires that the provider guarantee the value will remain valid after the accessor function has completed.

Yes, this does mean you can have a borrowing borrow which has both of these behaviors: The providing value itself is borrowed (appropriate when the containing value is non-copyable) and the property value is returned via borrow (which is appropriate when the property value is non-copyable).

I believe the use of borrowing as a modifier for methods, accessors, and function parameters was added in SE-0377.

Edit: I suppose the above might be a good reason to change the title of this proposal to “Borrow Accessors” rather than “Borrowing Accessors”

2 Likes