File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ import to_str:: * ;
2
+ import to_str:: to_str;
3
+
4
+ class cat implements to_str {
5
+ priv {
6
+ let mut meows : uint ;
7
+ fn meow ( ) {
8
+ #error ( "Meow" ) ;
9
+ self . meows += 1 u;
10
+ if self . meows % 5 u == 0 u {
11
+ self . how_hungry += 1 ;
12
+ }
13
+ }
14
+ }
15
+
16
+ let mut how_hungry : int ;
17
+ let name : str ;
18
+
19
+ new ( in_x : uint, in_y : int, in_name: str )
20
+ { self . meows = in_x; self . how_hungry = in_y; self . name = in_name; }
21
+
22
+ fn speak ( ) { self . meow ( ) ; }
23
+
24
+ fn eat ( ) -> bool {
25
+ if self . how_hungry > 0 {
26
+ #error ( "OM NOM NOM" ) ;
27
+ self . how_hungry -= 2 ;
28
+ ret true ;
29
+ }
30
+ else {
31
+ #error ( "Not hungry!" ) ;
32
+ ret false ;
33
+ }
34
+ }
35
+
36
+ fn to_str ( ) -> str { self . name }
37
+ }
38
+
39
+ fn print_out < T : to_str > ( thing : T , expected : str ) {
40
+ let actual = thing. to_str ( ) ;
41
+ #debug ( "%s" , actual) ;
42
+ assert ( actual == expected) ;
43
+ }
44
+
45
+ fn main ( ) {
46
+ let nyan : to_str = cat ( 0 u, 2 , "nyan" ) as to_str ;
47
+ print_out ( nyan, "nyan" ) ;
48
+ }
You can’t perform that action at this time.
0 commit comments