@@ -1127,30 +1127,40 @@ enum list<T> {
1127
1127
let a: list<int> = cons(7, @cons(13, @nil));
1128
1128
~~~~
1129
1129
1130
- ### Resources
1130
+ ### Classes
1131
1131
1132
- _ Resources_ are values that have a destructor associated with them. A
1133
- _ resource item_ is used to declare resource type and constructor.
1132
+ TODO: more about classes
1133
+
1134
+ _ Classes_ are named record types that may have a destructor associated
1135
+ with them, as well as fields and methods. For historical reasons, we
1136
+ may call a class with a destructor and a single field a "resource".
1137
+
1138
+ A _ class item_ declares a class type:
1134
1139
1135
1140
~~~~
1136
- resource file_descriptor(fd: libc::c_int) {
1137
- libc::close(fd);
1141
+ class file_descriptor {
1142
+ let fd: libc::c_int;
1143
+ new(fd: libc::c_int) { self.fd = fd; }
1144
+ drop { libc::close(self.fd); }
1138
1145
}
1139
1146
~~~~
1140
1147
1141
1148
Calling the ` file_descriptor ` constructor function on an integer will
1142
1149
produce a value with the ` file_descriptor ` type. Resource types have a
1143
- noncopyable [ type kind] ( #type-kinds ) , and thus may not be copied. The
1144
- semantics guarantee that for each constructed resources value, the
1145
- destructor will run once: when the value is disposed of (barring
1146
- drastic program termination that somehow prevents unwinding from taking
1147
- place). For stack-allocated values, disposal happens when the value
1148
- goes out of scope. For values in shared boxes, it happens when the
1149
- reference count of the box reaches zero.
1150
-
1151
- The argument to the resource constructor is stored in the resulting
1152
- value, and can be accessed using the dereference (` * ` ) [ unary
1153
- operator] ( #unary-operator-expressions ) .
1150
+ noncopyable [ type kind] ( #type-kinds ) , and thus may not be
1151
+ copied. Class types that don't have destructors may be copied if all
1152
+ their fields are copyable. The semantics guarantee that for each
1153
+ constructed resource value, the destructor will run once: when the
1154
+ value is disposed of (barring drastic program termination that somehow
1155
+ prevents unwinding from taking place). For stack-allocated values,
1156
+ disposal happens when the value goes out of scope. For values in
1157
+ shared boxes, it happens when the reference count of the box reaches
1158
+ zero.
1159
+
1160
+ The argument or arguments to the class constructor may be stored in
1161
+ the class's named fields, and can be accessed by a field reference. In
1162
+ this case, the ` file_descriptor ` 's data field would be accessed like
1163
+ ` f.fd ` , if ` f ` is a value of type ` file_descriptor ` .
1154
1164
1155
1165
### Interfaces
1156
1166
0 commit comments