From: "Panicz Maciej Godek" <godek.maciek@gmail.com>
To: "Clinton Ebadi" <clinton@unknownlamer.org>
Cc: guile-user@gnu.org, Neil Jerram <neiljerram@googlemail.com>
Subject: Re: Simplified slot access in goops
Date: Tue, 2 Dec 2008 20:30:11 +0100 [thread overview]
Message-ID: <1eb53f5d0812021130u5b41964pa20464a35ce67715@mail.gmail.com> (raw)
In-Reply-To: <87ljuyylav.fsf@unknownlamer.org>
Neil wtote:
>> I think this is a nice interface; but I'm less sure about the
>> implementation. Why do you need to use primitive-eval at all?
>>
>> Isn't CL's with-slots a bit like this? It might help to look at how
>> that is implemented.
Well, I've been having similar doubts, so I came up with something
which turned out to be similar to CLOS's with-slots. The previous solution
indeed required primitive-eval (or something similar), because it
wasn't just a syntax transformation -- that is, it invoked the class-slots
and class-of functions
The workaround requires the user to enumerate all the slots that
he or she is going to access. I wrote two macros, so you can write
(continuing the example from the previous letter):
(with-object-slots (o a b c)
(set! a 5)
(set! b (* a a))
(set! c (+ a b))
(list a b c))
=> (2 25 30)
you don't have to use the original slot names;
you can also use the `with-object-slots' macro supplying aliases:
(with-object-slots (o (x a) (y b) (z c))
(set! x (+ y z))
(list x y z))
=> (55 25 30)
the only problem is that you can't mix both ways, ie.
! (with-object-slots (o a (y b)) (+ a y))
would be incorrect (it is possible to implement such a macro,
but for now this one should suffice). It's still ok to write
(with-object-slots (o (a a) (y b)) (+ a y))
=> 80
The second macro, let-slots allows to make aliases for slots of more
than one object, for instance:
(define p (make C))
(let-slots ((o a b c)
(p (pa a) (pb b) (pc c)))
(set! a 5)
(set! pa a)
(+ pa a))
Everything should work just fine.
The implementation is based solely on R5RS macro system and the
make-procedure-with-setter, that is afaik specific to guile
Clinton wrote:
> with-slots uses symbol-macrolet[0] to bind each of the variable names to
> symbol macros within its body. I don't know of anything in Guile that is
> equivalent.
R5RS pattern language allows to create syntactic aliases for practically
any form. Some time ago I've found the following `let-alias' macro that does
just this:
(use-syntax (ice-9 syncase))
(define-syntax let-alias
(syntax-rules ()
((_ ((id alias) ...) body ...)
(let-syntax ((helper (syntax-rules ()
((_ id ...) (begin body ...)))))
(helper alias ...)))))
;; And here's the code for the aforementioned with-object-slots and let-slots:
(use-modules (oop goops))
(define slot-ref (make-procedure-with-setter slot-ref slot-set!))
(define-syntax with-object-slots (syntax-rules ()
((_ (object (alias1 slot1) ...) expr1 ...)
(let-alias ((alias1 (slot-ref
object (quote slot1))) ...)
expr1 ...))
((_ (object prop1 ...) expr1 ...)
(let-alias ((prop1 (slot-ref object
(quote prop1))) ...)
expr1 ...))))
(define-syntax let-slots (syntax-rules ()
((_ () expr1 ...)
(begin expr1 ...))
((_ (binding1 binding2 ...) expr1 ...)
(with-object-slots binding1
(let-slots (binding2 ...)
expr1 ...)))))
It is also possible to define the `with-slots' macro exactly
as it is in CLOS (but I think let-slots is fine)
Cheers
M.
next prev parent reply other threads:[~2008-12-02 19:30 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-11-27 20:36 Simplified slot access in goops Maciek Godek
2008-11-28 18:41 ` Panicz Maciej Godek
2008-12-01 22:25 ` Neil Jerram
2008-12-02 9:32 ` Clinton Ebadi
2008-12-02 19:30 ` Panicz Maciej Godek [this message]
2008-12-02 20:33 ` Clinton Ebadi
2010-08-28 20:07 ` Andy Wingo
2010-08-29 0:22 ` Panicz Maciej Godek
2010-08-29 0:56 ` Andy Wingo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1eb53f5d0812021130u5b41964pa20464a35ce67715@mail.gmail.com \
--to=godek.maciek@gmail.com \
--cc=clinton@unknownlamer.org \
--cc=guile-user@gnu.org \
--cc=neiljerram@googlemail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).