Skip to content

Commit 7eae204

Browse files
committed
Discuss classes in reference manual
Still could use work, but this is the best I've got for now.
1 parent 6081eb7 commit 7eae204

File tree

1 file changed

+130
-20
lines changed

1 file changed

+130
-20
lines changed

doc/rust.md

Lines changed: 130 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,11 +1106,12 @@ let a: list<int> = cons(7, @cons(13, @nil));
11061106

11071107
### Classes
11081108

1109-
TODO: more about classes
1110-
1111-
_Classes_ are named record types that may have a destructor associated
1112-
with them, as well as fields and methods. For historical reasons, we
1113-
may call a class with a destructor and a single field a "resource".
1109+
A _class_ is a named record type that collects together fields and
1110+
methods. It must have a _constructor_ (a function called `new` that
1111+
returns a new instance of the class), and may have a destructor (a
1112+
nullary function called `drop` that executes before the memory manager
1113+
frees the memory for a given class). For historical reasons, we may
1114+
call a class with a destructor and a single field a "resource".
11141115

11151116
A _class item_ declares a class type:
11161117

@@ -1123,21 +1124,130 @@ class file_descriptor {
11231124
~~~~
11241125

11251126
Calling the `file_descriptor` constructor function on an integer will
1126-
produce a value with the `file_descriptor` type. Resource types have a
1127-
noncopyable [type kind](#type-kinds), and thus may not be
1128-
copied. Class types that don't have destructors may be copied if all
1129-
their fields are copyable. The semantics guarantee that for each
1130-
constructed resource value, the destructor will run once: when the
1131-
value is disposed of (barring drastic program termination that somehow
1132-
prevents unwinding from taking place). For stack-allocated values,
1133-
disposal happens when the value goes out of scope. For values in
1134-
shared boxes, it happens when the reference count of the box reaches
1135-
zero.
1136-
1137-
The argument or arguments to the class constructor may be stored in
1138-
the class's named fields, and can be accessed by a field reference. In
1139-
this case, the `file_descriptor`'s data field would be accessed like
1140-
`f.fd`, if `f` is a value of type `file_descriptor`.
1127+
produce a value with the `file_descriptor` type.
1128+
1129+
_Fields_ are immutable by default, so instances of `file_descriptor`
1130+
can't have their `fd` fields reassigned. A mutable field declaration
1131+
looks like:
1132+
1133+
~~~~
1134+
let mut fd: libc::c_int;
1135+
~~~~
1136+
1137+
The only exception is that the body of the class constructor begins
1138+
with all the class's fields uninitialized, and is allowed to -- in
1139+
fact, must -- initialize all the fields. A special case in the
1140+
typestate pass enforces this invariant.
1141+
1142+
Usually, the class constructor stores its argument or arguments in the
1143+
class's named fields. In this case, the `file_descriptor`'s data field
1144+
would be accessed like `f.fd`, if `f` is a value of type
1145+
`file_descriptor`. By default, class fields are _public_: they can be
1146+
accessed both from methods inside the class, and code outside the
1147+
class. Classes can also have private fields:
1148+
1149+
~~~~
1150+
class file_descriptor {
1151+
let fd: *libc::FILE;
1152+
new(fd: *libc::FILE) {
1153+
self.fd = fd; self.name = none;
1154+
}
1155+
priv {
1156+
let mut name: option<str>;
1157+
}
1158+
fn get_name() -> str {
1159+
alt self.name {
1160+
none { fail "File has no name!"; }
1161+
some(n) { n }
1162+
}
1163+
}
1164+
}
1165+
~~~~
1166+
1167+
Private fields are instance-private: methods in a class `C` can access
1168+
`self`'s private fields, but not private fields of other values of
1169+
type `C`. Code outside a class can't access any private fields.
1170+
1171+
A class item may contain _methods_, which take an implicit `self`
1172+
argument:
1173+
1174+
~~~~
1175+
class file_descriptor {
1176+
let fd: *libc::FILE;
1177+
new(fd: *libc::FILE) { self.fd = fd; }
1178+
fn flush() {
1179+
libc::fflush(self.fd);
1180+
}
1181+
}
1182+
~~~~
1183+
1184+
In this case, ```open``` is a nullary method that calls the
1185+
```fopen``` function, defined in another library, on the ```fd```
1186+
field. As in this example, methods must refer to their self's fields
1187+
as fields of ```self```; bare references to ```fd``` can't
1188+
occur. Methods can be public or private; just like fields, they are
1189+
public by default and private if enclosed in a `priv` section.
1190+
1191+
Classes may be polymorphic:
1192+
1193+
~~~~
1194+
class file<A: copy> {
1195+
let data: A;
1196+
let fd: *libc::FILE;
1197+
new(data: A, fd: *libc::FILE) { self.data = data; self.fd = fd; }
1198+
}
1199+
~~~~
1200+
1201+
Methods may also be polymorphic, and can have additional type
1202+
parameters other than those bound in the class:
1203+
1204+
~~~~
1205+
class file<A: copy> {
1206+
let data: A;
1207+
let fd: *libc::FILE;
1208+
new(fd: *libc::FILE, data: A) { self.fd = fd; self.data = data; }
1209+
fn map_data<B>(f: fn(A) -> B) -> B {
1210+
f(self.data)
1211+
}
1212+
}
1213+
~~~~
1214+
1215+
Classes do not support inheritance, except through traits. As a
1216+
result, all class method dispatch is static (non-virtual).
1217+
1218+
A class may implement a trait (see [interfaces](#interfaces)):
1219+
1220+
~~~~
1221+
trait to_str {
1222+
fn to_str() -> str;
1223+
}
1224+
1225+
class file : to_str {
1226+
let fd: *libc::FILE;
1227+
new(fd: *libc::FILE) { self.fd = fd; }
1228+
fn to_str() -> str { "a file" }
1229+
}
1230+
~~~~
1231+
1232+
The syntax `class file: to_str` is pronounced "class `file`
1233+
implements trait `to_str`".
1234+
1235+
Class instances may be allocated on the stack, in the exchange heap,
1236+
or on the task heap. A value with a class type ```C``` has a
1237+
noncopyable [type kind](#type-kinds) if ```C``` has a destructor, and
1238+
thus may not be copied. Class types that don't have destructors may be
1239+
copied if all their fields are copyable.
1240+
1241+
The semantics guarantee that for each constructed resource value, the
1242+
destructor will run once: when the value is disposed of (barring
1243+
drastic program termination that somehow prevents unwinding from
1244+
taking place). For stack-allocated values, disposal happens when the
1245+
value goes out of scope. For values in shared boxes, it happens when
1246+
the reference count of the box reaches zero.
1247+
1248+
The order of fields in a class instance is significant; its runtime
1249+
representation is the same as that of a record with identical fields
1250+
laid out in the same order.
11411251

11421252
### Interfaces
11431253

0 commit comments

Comments
 (0)