Debian Codegraph @

examples/oper/overload_pre_post.ck

raw
1 // 1) example of overloading on objects that return primitives
2 // 2) simultaneous overloading of pre/post for ++
3 // 3) example of execution of pre/post increment (same order)
4 //
5 // requires: chuck-1.5.5.8 or higher
6
7 public class Foo
8 { 0 => int n; }
9
10 // unary pre overloading
11 // NOTE: '!' is no longer overloadable as of v1.5.5.8,
12 // in order to support conditional checks, e.g., if(!obj)
13 fun int @operator ~(Foo foo)
14 {
15 return !foo.n;
16 }
17
18 // unary pre overloading
19 fun int @operator ++(Foo foo)
20 {
21 return ++foo.n;
22 }
23
24 // unary postfix overloading
25 fun int @operator (Foo foo)++
26 {
27 return foo.n++;
28 }
29
30 // make foo
31 Foo a;
32 // should print: 1 1 2 2 2 3
33 <<< ~a, ++a, ++a, a.n, a++, a.n >>>;