Add
trait Add<T>
Source: compiler/src/builtin_docs/prelude/ops.hy The addition operator +.
a + b on a type with an Add instance calls add(a, b). int and
float have builtin instances (compiled to machine adds, not calls).
String concatenation with + is a separate rule and does not go through
Add. In a generic function, T: Add (or T: Num) allows + on T.
Required methods#
| Name | Description |
|---|---|
add | Returns a + b. |
Examples#
use tests::assert;
class V2 {
pub x: int,
pub y: int,
}
impl Add for V2 {
pub fn add(V2 a, V2 b) -> V2 {
return new V2(a.x + b.x, a.y + b.y);
}
}
fn main() {
let p = new V2(1, 2) + new V2(10, 20);
assert!(p.x == 11);
assert!(p.y == 22);
}