Updated `tests/syntax_callref.nit`
[nit.git] / tests / test_callref.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import functional
16
17 redef class Object
18 fun toto(x: Int): Int
19 do
20 return x + 1
21 end
22 end
23
24 redef class Int
25 redef fun toto(x) do return x + self
26
27 fun mult_by(x: Int): Int do return x * self
28 end
29
30 class A
31 fun fun1: String
32 do
33 return "in A::fun1"
34 end
35 end
36
37 class B
38 super A
39
40 redef fun fun1
41 do
42 return "in B::fun1"
43 end
44 end
45
46 class Counter
47 var x = 0
48 fun incr do x += 1
49 end
50
51 class C[E]
52 var x: E
53 redef fun to_s
54 do
55 if x != null then
56 return "x is {x.as(not null)}"
57 end
58 return "x is null"
59 end
60 end
61
62 var a = new A
63 var b: A = new B
64
65 var f1 = &a.fun1
66 print f1.call # "in A::fun1"
67
68 var f2 = &b.fun1
69 print f2.call # "in B::fun1"
70
71 var f3 = &10.mult_by
72 print f3.call(10) # 100
73
74 var f4 = &f2.call
75 print f4.call # "in B::fun1"
76
77 var f5: Fun0[Object] = &f4.call
78 print f5.call
79 print f5.call
80
81 print((&10.toto).call(100)) # 110
82 print((&"allo".toto).call(2)) # 3
83
84 var cnt = new Counter
85 var p1 = &cnt.incr
86 var ps = [p1,p1,p1,p1,p1]
87
88 for p in ps do p.call
89 print cnt.x # 5
90
91 var c1 = new C[nullable Object](null)
92 var c2 = new C[nullable Int](null)
93
94 var f6 = &c1.to_s
95 var f7 = &c2.to_s
96
97 print f6.call # "x is null"
98 print f7.call # "x is null"
99
100 c1.x = "test"
101 c2.x = 100
102
103 print f6.call # "x is test"
104 print f7.call # "x is 100"