guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: References/locations


From: Maciek Godek
Subject: Re: References/locations
Date: Mon, 11 Aug 2008 01:00:21 +0200

Kjetil:

>> Hi,
>> it's me again, asking silly questions.
>
> Not at all, your questions are very good. :-)

That's werid. Every time I write a post I've got the
strange feeling that I'm thinking in the way that
should be forbidden :)

I can give you the background of this idea.
I've been trying to implement a vector-based
object-oriented system (I'm rather reluctant towards
goops). I imagine that it could be used in the
following manner (borrowed from C++):

(define sphere
  (class
   () ;; public slots
   (x y radius) ;; private slots
   ((move (dx dy) ;; methods
          (set! x (+ x dx))
          (set! y (+ y dy)))
    (scale (factor)
           (set! radius (* radius factor))))))

Obviously, the "class" macro should convert methods
to lambdas. For "move" that would be:

  (lambda(self dx dy)
    (let((x (vector-loc self (get-hash-table properties 'x)))
           (y (vector-loc self (get-hash-table properties 'y)))
           (radius (vector-ref self (get-hash-table properties 'radius))))
      (set! x (+ x dx))
      (set! y (+ y dy)))

Obviously, the hash table reference would have to be
evaluated during macro expand to provide the maximum
efficiency.
(If you have any other solutions to this problem, I'm open
to suggestions)

>> Besides I think that the names "hash-ref"
>> and "vector-ref" are confusing, since they
>> don't return references, but values (therefore
>> the names like "vector-get" or "hash-get" would
>> be more apropreate)
>>
>
> Never thought about that, but it sounds
> correct. vector-get and hash-get would
> probably be mroe apropreate names.

On the other hand the world of scheme seems
to have gotten used to such convention.

> Well, here's a relatively clean way, I think:
>
> (define-macro (location name)
>   (define new-val (gensym))
>   `(lambda (,new-val)
>       (set! ,name ,new-val)))
>
> (define old-set! set!)
>
> (define-macro (set! a b)
>  `(if (procedure? ,a)  ;; Needs a better check.
>       (,a ,b)
>       (old-set! ,a ,b)))
>
>
> guile> (define x 10)
> guile> (define y (location x))
> guile> (set! y 20)
> guile> x
> 20
> guile>

That's amazing! Whenever I think of something (and imagine
how difficult would it be to implement), you come up with a
solution taking only a few lines of code :)
(I only have to think how would it perform in practice)

Thanks
M.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]