-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Multiple overloads of a single operator. #2961
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
Comments
In git HEAD, this example gets rejected for a different reason: now that we check for coherence, we no longer have impls that don't implement a trait. So you would have to change You could rewrite the code using a trait, but then the coherence checker would reject it, because you would be doing something like:
The coherence checker would reject the two instances of Right now it seems unlikely to me that we will support this form of overloading via traits. Among people on the core team, I think most of us think that overloading adds a lot of cognitive burden for people trying to read and understand new code. It's a good question, and one that the past decade or so of work on type class extensions in Haskell addresses, but in Rust we're trying to keep our goals modest ;-) |
Ahh, I didn't realise that that there were no function overloads at all in rust. Whilst it is slightly annoying as I want to be able to keep my code terse, I can see the reasoning in terms of cognitive burden. I may have missed it, but I didn't notice any mention of this in the tutorial. It might be worthwhile making this decision (and the reasoning behind it) a little more clear to people coming from other languages. You know, I almost wish there were no operator overloads now to save people from false expectations. I'll probably shift most of my overloads to plain old functions to keep things consistent. Ie. |
@bjz - We allow overloads. We don't allow overloads of the same function with different return types. This is, I believe, similar to Java's typechecking rules. |
Less than that even. We require overloads to differ by self (impl) type, and permit at most one use of a name per self type. |
As I pointed out on HN, I believe this works:
(Once Mul becomes a trait, of course.) Feel free to reopen if this doesn't work for some reason. |
…, r=dswij `trivially_copy_pass_by_ref` fixes fixes rust-lang#5953 fixes rust-lang#2961 The fix for rust-lang#5953 is overly aggressive, but the suggestion is so bad that it's worth the false negatives. Basically three things together: * It's not obviously wrong * It compiles * It may actually work when tested changelog: Don't lint `trivially_copy_pass_by_ref` when unsafe pointers are used. changelog: Better track lifetimes when linting `trivially_copy_pass_by_ref`.
Fixes needed due to renaming of a few items: - rust-lang#119063
Are multiple overloads of a single operator going to be supported in the future?
To illustrate what I mean here's a simple matrix implementation:
Here's a simple example that you can compile right away:
And this is the error I get when I compile using rust version 0.3:
The text was updated successfully, but these errors were encountered: