[plt-scheme] constructor in class.ss?

From: Robby Findler (robby at cs.uchicago.edu)
Date: Sat Oct 22 22:02:43 EDT 2005

At Sat, 22 Oct 2005 21:53:36 -0400, Jon Rafkind wrote:
> Im using the class system imported from class.ss and cant figure out how 
> to invoke a constructor of some sort on the object Im instantiating. 
> Actually my real problem is I want to initialize a field and add it to a 
> list at the same time. Something like
> (define foo%
>    (class* object% ()
>       (init-field (bar #f))
>       (define bars '())))
> (define f-instance (new foo% (bar 'tomato)))
> Should set f-instance's bar field to 'tomato and I want to add 
> f-instance's bar to f-instance's `bars' list. The reason for this is the 
> bar I am initializing f-instance with is special and I will add more 
> bar's to the bars list later on.

Is something wrong with this?

(define foo%
  (class* object% ()
    (init-field (bar #f))
    (define bars (list bar))
    (define/public (get-bars) bars)

(send (new foo% (bar 'tomato)) get-bars) ;; => (list 'tomato)

More generally, all of the code that is not in a method, but is in the
body of a class is the class's initializer. In this case, the
construction of the `bars' list of one element.


Posted on the users mailing list.