Rune - Content-locking work 1/2
[rune.git] / tests / lvalue.d
1 #!/usr/local/bin/rune -x
2 #
3 #       Demonstrate the use of lvalue scope.  lvalue scope may be applied
4 #       to individual procedural arguments or to the return value.  It
5 #       causes the caller to pass the object by reference instead of by
6 #       value, allowing the procedure to modify the contents of the
7 #       object.
8 #
9 #       This feature is most often used when implementing operators that
10 #       require lvalues, such as "++" and "&=", but it can be used to
11 #       great effect with normal procedures as well.
12
13 limport "sys";
14 import "stdio";
15
16 alias stdio.File *stdout = stdio.stdout;
17
18 class Vector2 {
19         int     r;
20         int     i;
21         method void plusone()
22         {
23                 ++this.r;
24         }
25 }
26
27 int
28 main(int ac, char **av)
29 {
30         (int a, int b) x;
31         int32_t i;
32         Vector2 c = ( 15, 16 );
33
34         x = (2, 50);
35         fubar(x, 100);
36
37 # this should fail because (1,1) is not an lvalue
38 #   fubar((1, 1));
39         
40         stdout->show("Should get 100:", x.b);
41
42         # The ++ operator should return an lvalue, so this should
43         # work.
44         #
45         i = 1;
46         ++ ++i;
47         stdout->show("should get 3:", i);
48
49         # 'this' in the method is an lvalue and can modify
50         # the object
51         #
52         c.plusone();
53         c.plusone();
54         stdout->show("should get 17:", c.r);
55
56         return(0);
57 }
58
59 # fubar() should be able to modify the caller's variable
60 #
61 void
62 fubar(lvalue (int a, int b) v, int w)
63 {
64         v.b = w;
65 }