[plt-scheme] constructor in class.ss?

From: Jon Rafkind (workmin at ccs.neu.edu)
Date: Sat Oct 22 22:24:16 EDT 2005

Robby Findler wrote:

>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)
>    (super-new)))
>(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.
Ah this works just as I didnt expect it to!( that is set bars to a list 
containing the value passed as an argument to (new) )

thanks a bunch

Posted on the users mailing list.