-
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
Conventions for the use of Deref
and DerefMut
#25
Conversation
This was discussed previously in rust-lang/rust#13126.
calls the `bar` function on the smart pointer type and `foo->bar()` calls the | ||
`bar` function on the wrapped type. Rust could change to emulate this setup. | ||
This would be a truly enormous change. `->` could be limited to use with | ||
`Deref` and `DerefMut`, but that ghettoizes smart pointers and would also |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could go the C route and only apply auto-deref via ->
on all pointer types (i.e. (~int).foo()
would be trying to call foo
on ~int
, but (~int)->foo()
would attempt to auto-deref.)
I'm starting to feel that a Perhaps |
I think UFCS basically addresses the problems we'll find in practice. There is no good reason we can't support autoderef for unsafe pointers, by the way. We just don't. |
My understanding of UFCS may be incomplete, but I don't see how it will significantly affect this at all. We will still be unable to extend the API of smart pointers, users will still find it confusing when the |
To be honest, UFCS isn't really needed at all. I think people implementing smart pointers should just not put methods on them but rather define functions. I don't consider function call syntax to be "obscure". It seems clear enough to say "if you want to operate on the pointer rather than its referent you use a function". I guess it depends on how common such methods are. My experience in C++ has been that they are really quite rare, but YMMV. I do think guidelines for when to implement Deref (and thus be considered a "smart pointer") are needed. It seems to boil down to a rule like "You should implement deref if your type is a largely transparent wrapper around some other type --- like a pointer. If you implement deref, you should not define methods of your own, but rather prefer functions." Actually, one of the things we talked about at the work week as part of UFCS actually makes the problem worse. We had talked about not requiring that a function "opt-in" to being used like a method but just accepting any function with a compatible first argument. But this seems to screw smart pointers that might want to offer operations (and define traits) that do not interefere with autoderef. So I think we'll want to revise this (cc @pcwalton) Ultimately I think that the idea of autoderef is completely incompatible with an overloadable |
@sfackler it could be that I am underestimating the problem, though -- I guess this will become slightly clearer in time. In particular, I could imagine that smart pointers types might want to implement traits that define methods, and this is a potential problem. |
I am beginning to wonder whether |
Niko has convinced me that |
Also this isn't really RFC-worthy I don't think. It's not proposing a change to the language etc. |
I think it's too early to adopt best practices. We may change our autoderef rules etc etc. (I have some ideas I want to toy with.) |
Instead of (This is my secondary preference after the variable-length |
How about allowing (or perhaps requiring) fields and functions of smart pointer types to be type-qualified? For example: rc.Rc::downgrade() |
As we discussed earlier about this, we think it's a little premature to have ultimatums about this. For now, we're going to close this, but I'm going to tag this as postponed because we're going to want to revisit this once we've got some more traction with using these traits. |
Fix link in table
This was discussed previously in rust-lang/rust#13126.