[plt-scheme] Need more class examples?

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Tue Nov 18 14:10:39 EST 2008

On Nov 18, 2008, at 1:38 PM, Veer wrote:

> Hello,
>
> This code :
>
> (define my-area% (class* object% (area<%>)
>                   (init min-width)
>                   (define mini-width min-width)
>
>                   (define/public (get-top-level-window)
>                     this)
>                   (define/public (get-parent)
>                     #f)
>                   (define/public (stretchable-height)
>                     #f)
>                   (define/public (stretchable-width)
>                     #f)
>                   (define/public (get-graphical-min-size)
>                     0)
>                   (define/public (min-width)
>                     0)
>                   (define/public (min-height)
>                     0)
>                   (super-new)))
>
> complains :
> "class*: duplicate declared identifier in: min-width "
>
> My question is how you implement class method and init variable with  
> same name?
> I tried to read the class reference manual , but its too complex for
> me at the moment, so some examples
> will be of great help.


If you make it a (public) field, you can use the "renamed" version of  
the 'init-field' syntax:

   (class* object% (area<%>)
     (init-field ((mini-width min-width)))
     ...)

That declares two things:
   - an initialization argument with an "external name" of 'min-width'
   - a field named 'mini-width', whose initial value comes from the  
initialization argument

If you don't want to make it a public field, then you can do this:

   (class* object% (area<%>)
     (init ((-min-width min-width)))
     (define mini-width -min-width)
     ...)

Then you'd have three names instead of just two:
   - 'min-width' is the external name of the initialization argument
   - '-min-width' is the internal name of the initialization argument
   - 'mini-width' is the name of the private instance variable

Ryan



>
>
> Thanks
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.