[plt-scheme] Class methods and class fields

From: Eddie Sullivan (eddieSull at hotmail.com)
Date: Tue Apr 29 09:29:05 EDT 2008

That gives some of the functionality, but doesn't specifically associate the 
variable with the class, and doesn't allow for inheritance the way structure 
type properties do. To add to your example, I can envision something like:

(define my-class%
  (class object%
    ;; Just initialized once ever:
    (static-field [db-connection (init-db)])
    (define/public (talk-to-db stuff) ... db-connection ...)

(define my-derived-class%
  (class my-class%
    ;; Provides access to superclass's static field:
    (inherit-static-field db-connection)
    (define/public (more-db-talking stuff) ... db-connection ...)

(let ([dbc (get-static-field db-connection my-derived-class%)])
  ... dbc ...)

----- Original Message ----- 
From: "Robby Findler" <robby at cs.uchicago.edu>
To: "Eddie Sullivan" <eddieSull at hotmail.com>
Cc: <plt-scheme at list.cs.brown.edu>
Sent: Tuesday, April 29, 2008 8:43 AM
Subject: Re: [plt-scheme] Class methods and class fields

> The PLT Scheme class system is embedded in an ordinary functional
> language, so you can just define functions or database connections
> outside the class an refer to those variables lexically, eg:
> (define db-connection (init-db))
> (define my-class%
>  (class object%
>    (define/public (talk-to-db stuff) ... db-connection ...)
>    (super-new)))
> Robby
> On Tue, Apr 29, 2008 at 7:38 AM, Eddie Sullivan <eddieSull at hotmail.com> 
> wrote:
>> Hi.
>>  I'm another long-time programmer trying out scheme. I have been working
>> with PLT's scheme/class module because I am used to
>>  the object oriented way of thinking about programming.
>>  In every other language I have used that has classes (Python and the C++
>>  family, also [incr tcl] but my memory of that is vague), there is the
>>  concept of "class variables", that is, variables that are associated 
>> with
>>  the class itself rather than with any particular instance, and the 
>> similar
>>  concept of "class methods." (They're also often called "static", but 
>> that
>>  term can be confusing, IMO.)
>>  It's easy to imagine scenarios where these concepts would be very 
>> useful,
>> such as a
>>  shared resource like a database connection, or simply a usage counter.
>>  Perhaps I'm misreading the documentation, but I can't find anything like
>> that in scheme/class. Structures have "structure type properties", so the
>> idea must be acknowledged to be useful in theory.
>>  Is it there and I'm missing it? If not, is there a simple way to 
>> implement
>> this functionality that I haven't figured out? Or is there a 
>> philosophical
>> reason why this was considered a bad idea?
>>  Thanks!
>>  -Eddie Sullivan
>>  _________________________________________________
>>   For list-related administrative tasks:
>>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.