[fluxus] OOP / polymorphism
Dave Griffiths
dave at pawfal.org
Mon Jul 17 01:55:40 PDT 2006
> Hi all,
>
> Inspired by: http://www.pawfal.org/index.php?page=SchemeThinking
>
> I made a dynamic method dispatcher, enabling polymorphism.
>
> See attachment, could be useful I hope.
Nice. I've been wondering about wrapping up fluxus calls into higher level
objects for teaching and livecoding - or whether it's a bad idea to put
another level in the way. I've been writing quite long fluxus scripts
lately, and I tend to do everything in an object orientated manner to make
it nice and encapulated, but do thing explicitly (objecttype-method object
arg1 arg2 ...)
There is an interesting way to do it using local state variables that I
remembered after writing that page that comes from the SICP book section
3:
http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-20.html#%_sec_3.1.1
Basically, your constructors can return a function with the object
embedded as local state:
---8<---
(define (make-account balance)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (dispatch m)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT"
m))))
dispatch)
Make-account can be used as follows:
(define acc (make-account 100))
((acc 'withdraw) 50)
50
((acc 'withdraw) 60)
"Insufficient funds"
((acc 'deposit) 40)
90
((acc 'withdraw) 60)
30
--->8---
cheers
dave
More information about the Fluxus
mailing list