| 1 |
// global operator overloading using the 'public' keyword |
| 2 |
// overloading an operator in one context, and using it in another |
| 3 |
|
| 4 |
// a custom class |
| 5 |
public class Foo |
| 6 |
{ 5 => int n; } |
| 7 |
|
| 8 |
// binary operator overloading |
| 9 |
// NOTE the use of 'public' keyword |
| 10 |
public int @operator +(Foo lhs, Foo rhs) |
| 11 |
{ |
| 12 |
return lhs.n + rhs.n; |
| 13 |
} |
| 14 |
|
| 15 |
// define unary operator overload for '~' |
| 16 |
fun int @operator ~( Foo foo ) |
| 17 |
{ return !foo.num; } |
| 18 |
|
| 19 |
// binary unary postfix overloading |
| 20 |
public int @operator (Foo foo) ++ |
| 21 |
{ |
| 22 |
return foo.n++; |
| 23 |
} |
| 24 |
|
| 25 |
// use the overloading in a different code context |
| 26 |
// (could be another file; here we use Machine.eval()) |
| 27 |
Machine.eval( "Foo a, b; <<<a++, ~a, a+b>>>;" ); |