projects: update some short descriptions
[nit.git] / examples / clock_more.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 # A beefed up example of the `clock` module that show class refinement.
16 #
17 # It refines clock to make them comparable.
18 module clock_more
19
20 import clock
21
22 redef class Clock
23 # Clock are now comparable
24 super Comparable
25
26 # Comparaison of a clock make only sense with an other clock
27 redef type OTHER: Clock
28
29 redef fun <(o)
30 do
31 # Note: < is the only abstract method of Comparable.
32 # All other operators and methods rely on < and ==.
33 return self.total_minutes < o.total_minutes
34 end
35 end
36
37 var c1 = new Clock(8, 12)
38 var c2 = new Clock(8, 13)
39 var c3 = new Clock(9, 13)
40
41 print "{c1}<{c2}? {c1<c2}"
42 print "{c1}<={c2}? {c1<=c2}"
43 print "{c1}>{c2}? {c1>c2}"
44 print "{c1}>={c2}? {c1>=c2}"
45 print "{c1}<=>{c2}? {c1<=>c2}"
46 print "{c1},{c2}? max={c1.max(c2)} min={c1.min(c2)}"
47 print "{c1}.is_between({c2}, {c3})? {c1.is_between(c2, c3)}"
48 print "{c2}.is_between({c1}, {c3})? {c2.is_between(c1, c3)}"
49
50 print "-"
51
52 c1.minutes += 1
53
54 print "{c1}<{c2}? {c1<c2}"
55 print "{c1}<={c2}? {c1<=c2}"
56 print "{c1}>{c2}? {c1>c2}"
57 print "{c1}>={c2}? {c1>=c2}"
58 print "{c1}<=>{c2}? {c1<=>c2}"
59 print "{c1},{c2}? max={c1.max(c2)} min={c1.min(c2)}"
60 print "{c1}.is_between({c2}, {c3})? {c1.is_between(c2, c3)}"
61 print "{c2}.is_between({c1}, {c3})? {c2.is_between(c1, c3)}"