d2ef89e2064f7e3b2b4e1b1f58060289b9f3ebfe
[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 # This module beef up the clock module by allowing a clock to be comparable.
16 # It show the usage of class refinement
17 module clock_more
18
19 import clock
20
21 redef class Clock
22 # Clock are now comparable
23 super Comparable
24
25 # Comparaison of a clock make only sense with an other clock
26 redef type OTHER: Clock
27
28 redef fun <(o)
29 do
30 # Note: < is the only abstract method of Comparable.
31 # All other operators and methods rely on < and ==.
32 return self.total_minutes < o.total_minutes
33 end
34 end
35
36 var c1 = new Clock(8, 12)
37 var c2 = new Clock(8, 13)
38 var c3 = new Clock(9, 13)
39
40 print "{c1}<{c2}? {c1<c2}"
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}? max={c1.max(c2)} min={c1.min(c2)}"
46 print "{c1}.is_between({c2}, {c3})? {c1.is_between(c2, c3)}"
47 print "{c2}.is_between({c1}, {c3})? {c2.is_between(c1, c3)}"
48
49 print "-"
50
51 c1.minutes += 1
52
53 print "{c1}<{c2}? {c1<c2}"
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}? max={c1.max(c2)} min={c1.min(c2)}"
59 print "{c1}.is_between({c2}, {c3})? {c1.is_between(c2, c3)}"
60 print "{c2}.is_between({c1}, {c3})? {c2.is_between(c1, c3)}"