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

Proposal: Implement assignment operator overloads #393

Closed
rust-highfive opened this issue Oct 13, 2014 · 7 comments
Closed

Proposal: Implement assignment operator overloads #393

rust-highfive opened this issue Oct 13, 2014 · 7 comments

Comments

@rust-highfive
Copy link

Issue by bjz
Sunday Apr 21, 2013 at 22:16 GMT

For earlier discussion, see rust-lang/rust#5992

This issue was labelled with: A-libs, A-traits, I-enhancement, P-low in the Rust repository


This would be very useful for core::num (see #4819), and also mathematics libraries.

// overloads `+=`
#[lang="add_assign"]
trait AddAssign<RHS> {
    fn add_assign(&mut self, &other: RHS);
}

// overloads `-=`
#[lang="sub_assign"]
trait SubAssign<RHS> {
    fn sub_assign(&mut self, &other: RHS);
}

// overloads `*=`
#[lang="mul_assign"]
trait MulAssign<RHS> {
    fn mul_assign(&mut self, &other: RHS);
}

// overloads `/=`
#[lang="quot_assign"]
trait QuotAssign<RHS> {
    fn quot_assign(&mut self, &other: RHS);
}

// overloads `%=`
#[lang="rem_assign"]
trait RemAssign<RHS> {
    fn rem_assign(&mut self, &other: RHS);
}

It would also be useful to be able to assign to values accessed via the index operator. This would return a mutable reference to the element. =, +=, -=, *=, /=, and %= would then be based off the overloads defined for that element type.

#[lang="index_assign"]
trait IndexAssign<Index,Element> {
    fn mut_index<'a>(&'a mut self, index: Index) -> &'a mut Element;
}

Edit: Removed Assign trait for = operator.

@kkimdev
Copy link

kkimdev commented Dec 27, 2014

Q: Why did you remove Assign trait for = operator from the RFC?

@eddyb
Copy link
Member

eddyb commented Dec 27, 2014

Overloading = would violate Rust's doctrine of "everything moves without side effects".

@mtahmed
Copy link

mtahmed commented Jan 3, 2015

(noob) Q: Does rust provide any way of doing something like this right now?:

let my_big_num: BigNum = 42;

Basically, to be able to assign "constants" to objects and have it work?
If not, would Assign trait have been the answer?
If yes, could you please explain a bit more why the Assign trait is bad?

@Kimundi
Copy link
Member

Kimundi commented Jan 3, 2015

Assign would be about modifying an existing instance of the type, not about creating a fresh one.

And I think for that, you'd rather want to push for making number literals generic, so that that turns into something like let my_big_num: BigNum = literal_from_integer(42);.

@japaric
Copy link
Member

japaric commented Mar 8, 2015

Implementation

RFC

@nagisa
Copy link
Member

nagisa commented Sep 2, 2015

Should default trait implementations like this be provided?

impl<R, T> AddAssign<R> for T 
where T: Add<R> {
    fn add_assign(&mut self, &rhs: R) { *self = self + rhs; }
}

👍 for this either way.

@alexcrichton
Copy link
Member

This is done!

wycats pushed a commit to wycats/rust-rfcs that referenced this issue Mar 5, 2019
Fixed some grammar and spelling
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

8 participants