[plt-scheme] Field names in the class system

From: Carl Eastlund (carl.eastlund at gmail.com)
Date: Sat Aug 1 11:08:21 EDT 2009

If you want a private field with the same name as an initialization
argument, you want to give the initialization argument a different
internal name.  It should work something like this:

(class
 ...
 (init ([name other-name] default))
 (define name other-name)
 ...)

You can leave out the default value, but you still need the extra
layer of parentheses that distinguish other-name from default.

--Carl

On Sat, Aug 1, 2009 at 11:04 AM, Todd O'Bryan<toddobryan at gmail.com> wrote:
> But doesn't that make the field public? Is there any way to do it
> without making the field public?
>
> On Sat, Aug 1, 2009 at 10:18 AM, Matthias Felleisen<matthias at ccs.neu.edu> wrote:
>>
>> Use init-field.
>>
>> On Aug 1, 2009, at 9:59 AM, Todd O'Bryan wrote:
>>
>>> How do I get the names I use when passing in values with new to match
>>> the internal field names?
>>>
>>> In other words, I'd like to be able to do
>>>
>>> (new foo% [name "blah"])
>>>
>>> and then assign the field name the value "blah" inside the class.
>>>
>>> My first attempt was:
>>>
>>> (define foo%
>>>  (class object%
>>>    (init name)
>>>
>>>    (super-new)
>>>
>>>    (define name name)))   ; doesn't work because name is already defined
>>>
>>> I tried using the maybe-renamed construct, but the name is backwards
>>> from what I expected:
>>>
>>> (define foo%
>>>  (class object%
>>>    (init (field-name name))
>>>    ...)
>>>
>>> means that I have to do (new foo% [field-name "blah"]), rather than
>>> [name "blah"].
>>>
>>> I'm sure I'm just missing something obvious...
>>>
>>> Todd


Posted on the users mailing list.