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

Numeric types should satisfy the Add trait #3980

Closed
bstrie opened this issue Nov 16, 2012 · 1 comment
Closed

Numeric types should satisfy the Add trait #3980

bstrie opened this issue Nov 16, 2012 · 1 comment
Labels
A-type-system Area: Type system

Comments

@bstrie
Copy link
Contributor

bstrie commented Nov 16, 2012

Right now if you write a generic implementation on all types that implement Add, like so:

impl<T: Copy Add<T,T>> Option<T> : Add<Option<T>, Option<T>> {

...attempting to use the implementation on numeric types fails because they haven't technically implemented Add. There should probably be some compiler magic to ensure that numeric types satisfy such generic bounds, even if they don't technically implement the trait.

@graydon
Copy link
Contributor

graydon commented Jun 6, 2013

Considering this impl shows up in libstd now:

impl<T: Copy + Add<T,T>> Add<Option<T>, Option<T>> for Option<T> {
    #[inline(always)]
    fn add(&self, other: &Option<T>) -> Option<T> {
        match (*self, *other) {
            (None, None) => None,
            (_, None) => *self,
            (None, _) => *other,
            (Some(ref lhs), Some(ref rhs)) => Some(*lhs + *rhs)
        }
    }
}

and that this program:

fn main () {
    println(fmt!("%?", Some(1) + Some(2)))
}

prints Some(3), I think this bug is now closed.

@graydon graydon closed this as completed Jun 6, 2013
RalfJung pushed a commit to RalfJung/rust that referenced this issue Oct 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

2 participants