2012-01-26 12:26:14 +01:00
|
|
|
type point = {x: int, y: int};
|
|
|
|
|
2012-01-26 15:52:28 +01:00
|
|
|
impl point_ops for point {
|
|
|
|
fn +(other: point) -> point {
|
2012-01-26 12:26:14 +01:00
|
|
|
{x: self.x + other.x, y: self.y + other.y}
|
|
|
|
}
|
2012-01-26 15:52:28 +01:00
|
|
|
fn -(other: point) -> point {
|
|
|
|
{x: self.x - other.x, y: self.y - other.y}
|
|
|
|
}
|
2012-01-27 10:06:53 +01:00
|
|
|
fn unary-() -> point {
|
2012-01-26 12:26:14 +01:00
|
|
|
{x: -self.x, y: -self.y}
|
|
|
|
}
|
2012-01-26 15:52:28 +01:00
|
|
|
fn [](x: bool) -> int {
|
2012-01-26 15:23:04 +01:00
|
|
|
x ? self.x : self.y
|
|
|
|
}
|
2012-01-26 12:26:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let p = {x: 10, y: 20};
|
2012-01-26 15:52:28 +01:00
|
|
|
p += {x: 101, y: 102};
|
|
|
|
p -= {x: 100, y: 100};
|
2012-01-26 12:26:14 +01:00
|
|
|
assert p + {x: 5, y: 5} == {x: 16, y: 27};
|
|
|
|
assert -p == {x: -11, y: -22};
|
2012-01-26 15:23:04 +01:00
|
|
|
assert p[true] == 11;
|
|
|
|
assert p[false] == 22;
|
2012-01-26 12:26:14 +01:00
|
|
|
}
|