source: project/release/4/prometheus/prometheus-2/examples/account.scm @ 14451

Last change on this file since 14451 was 14451, checked in by sjamaan, 11 years ago

Port Prometheus-2 to Chicken, using the clean, unmodified code from the release

File size: 1.3 KB
Line 
1;;; This is a simple account-keeping object.
2
3;;; It's just like a normal object
4(define account (*the-root-object* 'clone))
5
6;;; But it has a balance
7(account 'add-value-slot! 'balance 'set-balance! 0)
8
9;;; Which can be modified
10(account 'add-method-slot! 'payment!
11         (lambda (self resend amount)
12           (self 'set-balance!
13                 (+ (self 'balance)
14                    amount))))
15
16;;; Some tests:
17(define a1 (account 'clone))
18(define a2 (account 'clone))
19
20(a1 'payment! 100)
21(a2 'payment! 200)
22
23(a1 'balance)
24;;; => 100
25(a2 'balance)
26;;; => 200
27
28(a1 'payment! -20)
29(a1 'balance)
30;;; => 80
31
32;;; The typing for the slot definitions above can be rather tedious.
33;;; Prometheus provides syntactic sugar for those operations.
34
35;;; A method can be added with the DEFINE-METHOD syntax. This code is
36;;; equivalent to the code above which adds the PAYMENT! method:
37(define-method (account 'payment! self resend amount)
38  (self 'set-balance!
39        (+ (self 'balance)
40           amount)))
41
42;;; And this defines the whole object with the BALANCE slot and the
43;;; PAYMENT! method just as above:
44(define-object account (*the-root-object*)
45  (balance set-balance! 0)
46  ((payment! self resend amount)
47   (self 'set-balance!
48         (+ (self 'balance)
49            amount))))
Note: See TracBrowser for help on using the repository browser.