vile / examples / alt / records.vile
20 lines · 19 sloc · 583 bytes · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1; Each field clause names its accessor and, optionally, its mutator.
2define-record-type(point, make-point(x,y), point?, x(point-x,set-point-x!), y(point-y))
3p := make-point(20,22)
4set-point-x!(p,21)
5display(point?(p))
6newline()
7display(point-x(p)+point-y(p))
8newline()
9
10; Local declarations create a new type whenever the factory runs.
11factory := fn(value) {
12 define-record-type(cell, make-cell(value), cell?, value(cell-value))
13 [make-cell(value),cell?]
14}
15first := factory(42)
16second := factory(42)
17display(first[1](first[0]))
18newline()
19display(first[1](second[0]))
20newline()
21