Skip to content
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

Deref implementation for newtypes #18

Open
pip25 opened this issue Aug 9, 2018 · 1 comment
Open

Deref implementation for newtypes #18

pip25 opened this issue Aug 9, 2018 · 1 comment

Comments

@pip25
Copy link

pip25 commented Aug 9, 2018

First of all, thanks for the great summary of the orphan rule and its surrounding issues, it's been very helpful for me as I tried to understand the overall topic.

One thing the text and linked issues do not seem to mention is using Deref for newtypes to avoid boilerplate. Now I understand that this topic might be controversial, the unofficial rust pattern repo for instance mentions implementing Deref in this case as an anti-pattern, but on the other hand the much more recent second edition of the Rust introductory book now mentions using Deref on newtypes as a practical solution.

So, what is the current consensus on this subject? Is it such a non-starter that it's not even worth mentioning, or was it not included here for some other reason? Because, while I'm definitely a beginner in Rust, to me the whole thing seems pretty tempting: you do not need to write delegate methods pretty much at all, you can implement new traits on the newtype as normal, and you can also use the trait implementations of the wrapped type with the dereference operator (*).

@Ixrec
Copy link
Owner

Ixrec commented Aug 9, 2018

Excellent point, I did not think about Deref at all when I was doing these write ups.

My vague, mostly baseless impression is that using Deref on newtypes is par for the course today (despite the various downsides), but nobody believes that Deref is part of the ideal solution for any of these use cases, so it'll probably start to fade away as features like specialization and delegation get fully implemented and stabilized (assuming they really do solve these problems).

That said, I can't recall anyone ever proposing an "ideal solution" that involved Deref, so I can't rule out the possibility that we've all missed something. And half the point of this repo is to gather concrete use cases so we can actually answer questions like "are there any important use cases specialization and delegation can't solve?" More ideas are always welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants