-
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
Struct properties #419
Comments
It'd have to be a bit more than just a getter and setter to allow migration from a field to a property without breaking anything, since code that took a reference to the field would break. |
That could potentially be covered by the "reference with appropriate lifetime" version of get/set. In the absence of that, for some structure fields it may work to simply produce a compile-time error on an attempt to take a reference, as though attempting to take a reference of an rvalue. |
@joshtriplett how does one return a reference to a value that is computed by a getter? Also pattern matching/destructuring might be tricky. |
I suppose temporary locals could be made at the call-site to store the value for destructuring and references? |
The getter could return a reference with the same lifetime as the structure the getter is called on.
How so? Seems straightforward enough to pattern match or destructure on the return value of a function. |
Where would that reference point? |
Shouldn't it have access to
How so? Assuming it works without references, taking a reference to the return value of a function |
@fread2281 |
@sfackler Sure, but properties are still useful, and I don't think there's a way to make getters that allocate their return value lifetime-compatible (where do you insert the |
@joshtriplett still interested in this or shall we close? |
* Update deprecation-template.md * Update deprecation-template.md
This is an old issue but as a Rust user coming from Swift's computed properties, it can be very handy at times for abstracting away small computations or adjustments in a type! |
I think this issue should be close in favor of https://github.com/nikomatsakis/fields-in-traits-rfc because methods cannot addresses the related borrowing concerns without partial borrowing #1215 (comment) |
This can be done with macros, isn't? So, I want you (or moderation) to close this, please. I don't think that this feature is really worth it |
I'd like to have syntax for defining named "properties" on a struct. A property would consist of a name, a type (which may use in-scope generics), an optional get function (taking void and returning the field type), and an optional set function. (The name and the get/set function can each independently have "pub" visibility or not.) A property will not have any storage allocated for it in the struct layout; instead, attempts to get or set the property will become calls to the get or set function. A property with no get or set function specified will produce a compile-time error on an attempt to get or set it, respectively.
The get function would take no arguments and return a value of the field type, and the set function would accept a value of the field type and return nothing. In both cases, it would also work to accept/return a reference with appropriate lifetime.
In addition to the convenient syntax and encapsulation, this would then support several useful cases. Most notably, it would support migrating an existing field to a property, removing it from the structure layout while maintaining compatibility with previous code that accessed it via field syntax.
The text was updated successfully, but these errors were encountered: