[plt-scheme] class fields and accessors

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Fri Sep 13 16:45:52 EDT 2002

At Fri, 13 Sep 2002 13:32:14 -0400, "Mike T. Machenry" wrote:
>   In the documentation for many of the mred classes, they have initilization
> arguments and access/mutate'ers of the same name. For example
> horizontal-panel% has an initialization argument for spacing and has a
> method spacing which (based on its arguments) either accesses or mutates
> the field initilized by spacing. When I attempt a simple class to do the
> same:
> 
> (require (lib "class.ss"))
> (define example%
>   (class object%
>     (init-field spacing)
>     (define/public spacing
>       (case-lambda
>         [(new-spacing) (set! spacing new-spacing)]
>         [() spacing]))
>     (super-instantiate ())))
> 
> I get the error "class*/names: duplicate declared identifier in: spacing"
> Is there a proper way to work around this? 

The MrEd classes sometimes have an init argument (declared with `init')
using the same name as a method, but it's not a field. There's no way
to have a field and method with the same name.

The implementation of the MrEd class is something like

 (class ...
     (init spacing)
     (define private-spacing-field spacing)
     (define spacing-method
       (case-lambda
        [() private-spacing-field]
        [(v) (set! private-spacing-field v)]))
     (public (spacing-method spacing))
      ...)

Note that the method has to be renamed, while it would be nicer to have
a different internal name for the init argument. I keep forgetting to
add a renaming form for init arguments and fields.

Matthew




Posted on the users mailing list.