Skip to content

Instantly share code, notes, and snippets.

@bstrie
Created November 15, 2012 20:25
Show Gist options
  • Select an option

  • Save bstrie/4081036 to your computer and use it in GitHub Desktop.

Select an option

Save bstrie/4081036 to your computer and use it in GitHub Desktop.
Overloading + on Option
compile_and_link: x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib/libcore.so
/media/linhaus/rust/src/libcore/option.rs:331:46: 331:48 error: mismatched types: expected `'a` but found `'b` (expected type parameter but found type parameter)
/media/linhaus/rust/src/libcore/option.rs:331 (Some(v1), Some(v2)) => Some(v1 + v2)
^~
/media/linhaus/rust/src/libcore/option.rs:331:36: 332:9 error: mismatched types: expected `option::Option<'b>` but found `option::Option<'a>` (expected type parameter but found type parameter)
/media/linhaus/rust/src/libcore/option.rs:331 (Some(v1), Some(v2)) => Some(v1 + v2)
/media/linhaus/rust/src/libcore/option.rs:332 }
error: aborting due to 2 previous errors
make: *** [x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib/libcore.so] Error 101
impl<R,T: Add<R,R>> Option<T> : Add<Option<T>, Option<T>> {
#[inline(always)]
pure fn add(other: &Option<T>) -> Option<T> {
match (self, *other) {
(None, None) => None,
(_, None) => self,
(None, _) => *other,
(Some(v1), Some(v2)) => Some(v1 + v2)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment