Skip to content

Proposal: Implement assignment operator overloads #5992

Closed
@brendanzab

Description

@brendanzab

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.P-lowLow priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions