@@ -211,7 +211,7 @@ The keywords in [source files](#source-files) are the following strings:
211
211
~~~~~~~~ {.keyword}
212
212
again assert
213
213
break
214
- check const copy
214
+ check class const copy
215
215
drop
216
216
else enum export extern
217
217
fail false fn for
@@ -220,7 +220,6 @@ let log loop
220
220
match mod mut
221
221
pure
222
222
return
223
- struct
224
223
true trait type
225
224
unchecked unsafe
226
225
while
@@ -1098,6 +1097,151 @@ enum list<T> {
1098
1097
let a: list<int> = cons(7, @cons(13, @nil));
1099
1098
~~~~
1100
1099
1100
+ ### Classes
1101
+
1102
+ A _ class_ is a named record type that collects together fields and
1103
+ methods. It must have a _ constructor_ (a function called ` new ` that
1104
+ returns a new instance of the class), and may have a destructor (a
1105
+ nullary function called ` drop ` that executes before the memory manager
1106
+ frees the memory for a given class). For historical reasons, we may
1107
+ call a class with a destructor and a single field a "resource".
1108
+
1109
+ A _ class item_ declares a class type:
1110
+
1111
+ ~~~~
1112
+ class file_descriptor {
1113
+ let fd: libc::c_int;
1114
+ new(fd: libc::c_int) { self.fd = fd; }
1115
+ drop { libc::close(self.fd); }
1116
+ }
1117
+ ~~~~
1118
+
1119
+ Calling the ` file_descriptor ` constructor function on an integer will
1120
+ produce a value with the ` file_descriptor ` type.
1121
+
1122
+ _ Fields_ are immutable by default, so instances of ` file_descriptor `
1123
+ can't have their ` fd ` fields reassigned. A mutable field declaration
1124
+ looks like:
1125
+
1126
+ ~~~~
1127
+ let mut fd: libc::c_int;
1128
+ ~~~~
1129
+
1130
+ The only exception is that the body of the class constructor begins
1131
+ with all the class's fields uninitialized, and is allowed to -- in
1132
+ fact, must -- initialize all the fields. The compiler enforces this
1133
+ invariant.
1134
+
1135
+ Usually, the class constructor stores its argument or arguments in the
1136
+ class's named fields. In this case, the ` file_descriptor ` 's data field
1137
+ would be accessed like ` f.fd ` , if ` f ` is a value of type
1138
+ ` file_descriptor ` . By default, class fields are _ public_ : they can be
1139
+ accessed both from methods inside the class, and code outside the
1140
+ class. Classes can also have private fields:
1141
+
1142
+ ~~~~
1143
+ class file_descriptor {
1144
+ let fd: *libc::FILE;
1145
+ new(fd: *libc::FILE) {
1146
+ self.fd = fd; self.name = none;
1147
+ }
1148
+ priv {
1149
+ let mut name: option<~str>;
1150
+ }
1151
+ fn get_name() -> ~str {
1152
+ match self.name {
1153
+ none => fail ~"File has no name!",
1154
+ some(n) => n
1155
+ }
1156
+ }
1157
+ }
1158
+ ~~~~
1159
+
1160
+ Private fields are instance-private: methods in a class ` C ` can access
1161
+ ` self ` 's private fields, but not private fields of other values of
1162
+ type ` C ` . Code outside a class can't access any private fields.
1163
+
1164
+ A class item may contain _ methods_ , which take an implicit ` self `
1165
+ argument:
1166
+
1167
+ ~~~~
1168
+ class file_descriptor {
1169
+ let fd: *libc::FILE;
1170
+ new(fd: *libc::FILE) { self.fd = fd; }
1171
+ fn flush() {
1172
+ libc::fflush(self.fd);
1173
+ }
1174
+ }
1175
+ ~~~~
1176
+
1177
+ In this case, ``` open ``` is a nullary method that calls the
1178
+ ``` fopen ``` function, defined in another library, on the ``` fd ```
1179
+ field. As in this example, methods must refer to their self's fields
1180
+ as fields of ``` self ``` ; bare references to ``` fd ``` can't
1181
+ occur. Methods can be public or private; just like fields, they are
1182
+ public by default and private if enclosed in a ` priv ` section.
1183
+
1184
+ Classes may be polymorphic:
1185
+
1186
+ ~~~~
1187
+ class file<A: copy> {
1188
+ let data: A;
1189
+ let fd: *libc::FILE;
1190
+ new(data: A, fd: *libc::FILE) { self.data = data; self.fd = fd; }
1191
+ }
1192
+ ~~~~
1193
+
1194
+ Methods may also be polymorphic, and can have additional type
1195
+ parameters other than those bound in the class:
1196
+
1197
+ ~~~~
1198
+ class file<A: copy> {
1199
+ let data: A;
1200
+ let fd: *libc::FILE;
1201
+ new(fd: *libc::FILE, data: A) { self.fd = fd; self.data = data; }
1202
+ fn map_data<B>(f: fn(A) -> B) -> B {
1203
+ f(self.data)
1204
+ }
1205
+ }
1206
+ ~~~~
1207
+
1208
+ Classes do not support inheritance, except through traits. As a
1209
+ result, all class method dispatch is static (non-virtual).
1210
+
1211
+ A class may implement a trait (see [ traits] ( #traits ) ):
1212
+
1213
+ ~~~~
1214
+ trait to_str {
1215
+ fn to_str() -> ~str;
1216
+ }
1217
+
1218
+ class file : to_str {
1219
+ let fd: *libc::FILE;
1220
+ new(fd: *libc::FILE) { self.fd = fd; }
1221
+ fn to_str() -> ~str { ~"a file" }
1222
+ }
1223
+ ~~~~
1224
+
1225
+ The syntax ` class file: to_str ` is pronounced "class ` file `
1226
+ implements trait ` to_str ` ".
1227
+
1228
+ Class instances may be allocated on the stack, in the exchange heap,
1229
+ or on the task heap. A value with a class type ``` C ``` has a
1230
+ noncopyable [ type kind] ( #type-kinds ) if ``` C ``` has a destructor, and
1231
+ thus may not be copied. Class types that don't have destructors may be
1232
+ copied if all their fields are copyable.
1233
+
1234
+ The semantics guarantee that for each constructed resource value, the
1235
+ destructor will run once: when the value is disposed of (barring
1236
+ drastic program termination that somehow prevents unwinding from
1237
+ taking place). For stack-allocated values, disposal happens when the
1238
+ value goes out of scope. For values in shared boxes, it happens when
1239
+ the reference count of the box reaches zero.
1240
+
1241
+ The order of fields in a class instance is significant; its runtime
1242
+ representation is the same as that of a record with identical fields
1243
+ laid out in the same order.
1244
+
1101
1245
### Traits
1102
1246
1103
1247
A _ trait item_ describes a set of method types. [ _ implementation
@@ -1204,7 +1348,7 @@ trait. The methods in such an implementation can only be used
1204
1348
statically (as direct calls on the values of the type that the
1205
1349
implementation targets). In such an implementation, the ` of ` clause is
1206
1350
not given, and the name is mandatory. Such implementations are
1207
- limited to nominal types (enums, structs ) and the implementation must
1351
+ limited to nominal types (enums, classes ) and the implementation must
1208
1352
appear in the same module or a sub-module as the receiver type.
1209
1353
1210
1354
_ When_ a trait is specified, all methods declared as part of the
@@ -2600,9 +2744,9 @@ fn main() {
2600
2744
In this example, the trait ` printable ` occurs as a type in both the type signature of
2601
2745
` print ` , and the cast expression in ` main ` .
2602
2746
2603
- ### Struct types
2747
+ ### Class types
2604
2748
2605
- Every struct item defines a type.
2749
+ Every class item defines a type. See [ classes ] ( #classes ) .
2606
2750
2607
2751
### Type parameters
2608
2752
@@ -2622,7 +2766,7 @@ type `~[B]`, a vector type with element type `B`.
2622
2766
2623
2767
### Self type
2624
2768
2625
- The special type ` self ` has a meaning within methods inside an
2769
+ The special type ` self ` has a meaning within methods inside a class or
2626
2770
impl item. It refers to the type of the implicit ` self ` argument. For
2627
2771
example, in:
2628
2772
@@ -2637,7 +2781,19 @@ impl ~str: printable {
2637
2781
~~~~~~
2638
2782
2639
2783
` self ` refers to the value of type ` str ` that is the receiver for a
2640
- call to the method ` to_str ` .
2784
+ call to the method ` to_str ` . Similarly, in a class declaration:
2785
+
2786
+ ~~~~~~
2787
+ class cat {
2788
+ let mut meows: uint;
2789
+ new() { self.meows = 0; }
2790
+ fn meow() { self.meows = self.meows + 1; }
2791
+ }
2792
+ ~~~~~~
2793
+
2794
+ ` self ` refers to the class instance that is the receiver of the method
2795
+ (except in the constructor ` new ` , where ` self ` is the class instance
2796
+ that the constructor implicitly returns).
2641
2797
2642
2798
## Type kinds
2643
2799
0 commit comments