-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Operator overloading not flexible enough? #21188
Comments
I think |
And it's impossible to implement both |
Yes. But this is just an example - what about something that is expensive to clone, like a You just have to implement all combinations of And I did still not figure out how you can define an overloaded operator for the RHS, i.e. |
CC @aturon |
The code a + b + b + a is equivalent to ((a + b) + b) + a where the inner &(&(a + b) + b) + a This option is not really desirable in my opinion. Option 2 would be to add at least another implementation: impl<'a> Add<&'a Point> for Point { … } (and possibly the other way around for symmetry). Providing all these impls is a bit of pain in the butt. But I think macros can mitigate this to some extend. For example, we could try to standardize a macro for |
@nikomatsakis and I have been talking about ways to address this sort of thing by allowing you to write just the by-ref implementations of the operators and having the compiler automatically insert |
I think the scheme we've settled on here is to implement the operator traits for the 4 referenced/non-referenced combinations, i.e. impl Add<Point> for Point { ... }
impl<'a> Add<Point> for &'a Point { ... }
impl<'a> Add<&'a Point> for Point { ... }
impl<'a> Add<&'a Point> for &'a Point { ... } This is somewhat annoying, but for a simple/cheap type like I'm inclined to close this, despite us not having the auto-ref idea @aturon mentions, that is, one has to write something like |
@huonw I have no objection to closing on that basis. I'd still love to have some for of auto-ref, but it's an RFC concern at this point. |
I might just missing some informations here, but I have a hard time implementing a simple Point type that can be added:
That error makes sense to me, so I went ahead and tried implementing this for &'a Point.
But now chaining does no longer work since the Add returns a
Point
, not a&mut Point
. Can somebody enlighten me about the correct way of solving this?Another - maybe unrelated - issue I stumbled on is how to make
Point + f32
do the same asf32 + Point
.The text was updated successfully, but these errors were encountered: