scsh-users
[Top] [All Lists]

Re: Implementing classes in scheme.

To: scsh-news@zurich.ai.mit.edu
Subject: Re: Implementing classes in scheme.
From: Taylor Campbell <spam.address@dont.use>
Date: Sat, 30 Nov 2002 02:23:50 GMT
Tom Booster wrote:
> I attempting to implement classes in scheme with using 
> (let-lambda-let-lambda) this isn't too difficult. My problem is 
> attempting to implement this.* of every object.
> For example I can code a function to increment my class variables.
> (set! this.counter (+ this.counter 1))
> Does anyone have a clue of how to implement this class in scheme?
> Thanks Tom
> 

Scheme doesn't actually have 'classes'; what you're using is instead a
'closure.'  A closure captures its current environment and uses it in
its body, to produce things like instance variables.  To capture a
'class variable,' (quoted because it's not really a 'class variable' in
the OO sense) you might use:

(define make-point
   (let ((default 0))
     (lambda ()
       (let ((x default) (y default))
         <instance message dispatcher>))))

Here the make-point function captures 'default.'

Because, however, Scheme doesn't have 'classes,' it doesn't have a dot
syntax; you would merely reference 'default' with 'default'.

By the way, this should be on c.l.scheme, not c.l.s.scsh, as it's not
scsh-specific.


<Prev in Thread] Current Thread [Next in Thread>