| 1 |
// verify that binary, unary-pre, and unary-post overloadings that |
| 2 |
// return Objects are correctly ref-counted across op func calls |
| 3 |
|
| 4 |
// let's say we define a custom class... |
| 5 |
public class Foo { int num; } |
| 6 |
|
| 7 |
// persistent operator overloading (trascends contexts) |
| 8 |
// NOTE the use of 'public' instead of 'fun' -- it's fun for all! |
| 9 |
public Foo @operator =^( Foo lhs, Foo rhs ) |
| 10 |
{ /* do stuff for Foo =^ Foo */ return rhs; } |
| 11 |
|
| 12 |
// LOCAL binary operator overload for '=>' (local to this context) |
| 13 |
fun Foo @operator =>( Foo lhs, Foo rhs ) |
| 14 |
{ /* do stuff for Foo => Foo */ return rhs; } |
| 15 |
|
| 16 |
// define binary operator overload for '+' |
| 17 |
fun Foo @operator +( Foo lhs, Foo rhs ) |
| 18 |
{ Foo retval; lhs.num + rhs.num => retval.num; return retval; } |
| 19 |
|
| 20 |
// define binary operator overload for '*' |
| 21 |
fun Foo @operator *( Foo lhs, Foo rhs ) |
| 22 |
{ Foo retval; lhs.num * rhs.num => retval.num; return retval; } |
| 23 |
|
| 24 |
// define unary operator overload for '++' |
| 25 |
fun int @operator ++( Foo foo ) |
| 26 |
{ return ++foo.num; } |
| 27 |
|
| 28 |
// define postfix operator overload for '++' |
| 29 |
fun Foo @operator ( Foo foo ) ++ |
| 30 |
{ foo.num++; return foo; } |
| 31 |
|
| 32 |
//----------------------------------------------------------- |
| 33 |
// using the new overloaded operators |
| 34 |
//----------------------------------------------------------- |
| 35 |
// create 2 Foos |
| 36 |
Foo a, b; 1 => a.num; 2 => b.num; |
| 37 |
// operator in action (follows default ck operator precedence) |
| 38 |
a + b * b + a @=> Foo @ c; // note c is not a new instance |
| 39 |
// post ++ on c |
| 40 |
c++; |
| 41 |
// another instance |
| 42 |
Foo d; |
| 43 |
// post ++ on d |
| 44 |
d++; |
| 45 |
|
| 46 |
// should print 1s |
| 47 |
if( Machine.refcount(a) == 1 && |
| 48 |
Machine.refcount(b) == 1 && |
| 49 |
Machine.refcount(c) == 1 && |
| 50 |
Machine.refcount(d) == 1 ) <<< "success" >>>; |