AW: AW: [plt-scheme] Changing field values in objects

From: Robert Matovinovic (robert.matovinovic at web.de)
Date: Fri Feb 2 10:29:17 EST 2007

Thanks Matthias,
that looks quiet simple and strait forward. I only got hooked by the
get-field procedure and thought there must be something similar to it. Is
there a reason why there isn't?

Robert

-----Ursprüngliche Nachricht-----
Von: plt-scheme-bounces at list.cs.brown.edu
[mailto:plt-scheme-bounces at list.cs.brown.edu] Im Auftrag von Matthias
Felleisen
Gesendet: Friday, February 02, 2007 13:31
An: Robert Matovinovic
Cc: plt-scheme at list.cs.brown.edu; 'Michael Vanier'
Betreff: Re: AW: [plt-scheme] Changing field values in objects


Why not

  (define foo%
    (class object%
      ...
      (define x 0)
      (define/public (x-setter x-val) (set! x x-val))
      ...))




On Feb 2, 2007, at 8:01 AM, Robert Matovinovic wrote:

> Thank you Michael,
> You're right, it works but somehow it looks not very elegant. I
> experimented
> with it and came up with the following define-syntax:
>
> (define-syntax set-field!
>   (syntax-rules ()
>     ((set-field! f c obj val) ((class-field-mutator c f) obj val))
>     )
>   )
>
> Now I can write (set-field! value test% t 20)
>
> This is a bit more general, because I now can access every field.
> What is
> still a little bit weird is that I need the class explicitly, since
> (object-info t) -> #f #t. The #f means that it doesn't know the  
> class. How
> do I get the class of an object, if not via that way?
>
> Robert
>
> -----Ursprüngliche Nachricht-----
> Von: plt-scheme-bounces at list.cs.brown.edu
> [mailto:plt-scheme-bounces at list.cs.brown.edu] Im Auftrag von
> Michael Vanier
> Gesendet: Friday, February 02, 2007 12:27
> An: Robert Matovinovic
> Cc: plt-scheme at list.cs.brown.edu
> Betreff: Re: [plt-scheme] Changing field values in objects
>
>
> This isn't a nice way of doing it, but this works:
>
> <your code, then ...>
>
> (define set-value! (class-field-mutator test% value)) (set-value! t  
> 20)
>
> The nice way, I presume would be to define a public accessor method.
>
> I got this from the class.ss documentation in the libraries  
> manual.  Given
> my
> very casual perusal, I will say that it looks like it doesn't serve  
> well as
> a
> tutorial.
>
> Mike
>
> Robert Matovinovic wrote:
>> Hi,
>> I'm nearly desparate, since I'm trying to do a simple thing: changing
>> the value of a field in an object. I read the help desk thoroughly  
>> and
>> tried a lot, but couldn't find a solution, which to most of you must
>> be obvious. So here is a bit of sample code. I would appreciate very
>> much if someone can just add the lines to change the value of the
>> value field:
>>
>> (define test%
>>   (class object%
>>     (init-field (value 0))
>>     (super-new)
>>     )
>>   )
>> (define t (new test% (value 10)))
>> (get-field value t)
>>
>> BTW, is there any tutorial to familiarize oneself easily with the
>> object system of plt scheme?
>>
>> Thanks in advance
>> Robert
>>
>> _________________________________________
>> Robert Matovinovic
>> 2 Fenella Drive, Monavale
>> Harare
>> Zimbabwe
>>
>> Tel:    +263 (0)4 339 879
>> Cell:   +263 (0)91 43 66 47
>> email: robert.matovinovic at web.de
>>
>> _________________________________________________
>>   For list-related administrative tasks:
>>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme

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



Posted on the users mailing list.