menu
149 tokens
Operator Overloading
Serious math-y applications want operator overloading.
Operator Overloading
- operator overloading
- serious math-y applications want operator overloading
- newtype interfaces ("traits")
- binary
Add,Subtract,Multiply,Divide, etc. - unary
Plus,Negate - operators dispatch through standard interfaces on the left operand only; the reverse operand order needs its own implementation
1import { Add } from "destack:ops";2 3struct Vector2 { x: float64; y: float64; }4 5extension of Vector2 implements Add<Vector2> {6 type Output = Vector2;7 add(this, other: Vector2): Vector2 {8 Vector2 { x: this.x + other.x, y: this.y + other.y }9 }10}