From: Robert Gaimari
Subject: Get slot names from struct instance?
Date:
Message-ID: <8ekvcs$kes$1@news3.bu.edu>
Hi,
I'm having trouble finding the answer to this question and hope someone
can help me out. Chances are there is an easy answer, and I'm just not
seeing it.
I need to be able to find the slot names of a structure from an instance
of it. I don't have access to changing the structure definitions to
accommodate this, as there is a lot of code written by other people. For
example, if I have:
(defstruct foo
(a nil)
(b nil))
(setf bar (make-foo :a 'x :b 'y))
I need to have something like:
(slot-names bar)
=> (a b) ; return value
I'm using Allegro 5.0, under Solaris and eventually MS Windows. Thanks.
- Bob Gaimari
In article <············@news3.bu.edu>,
Robert Gaimari <········@cs.bu.edu> wrote:
>I'm having trouble finding the answer to this question and hope someone
>can help me out. Chances are there is an easy answer, and I'm just not
>seeing it.
There's no standard way. Apparently you didn't try looking in the FAQ,
since I believe this was one of the frequently-asked questions that
prompted us to create it in the first place.
--
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
In article <············@news3.bu.edu>, Robert Gaimari
<········@cs.bu.edu> wrote:
> I need to be able to find the slot names of a structure from an instance
> of it. I don't have access to changing the structure definitions to
> accommodate this, as there is a lot of code written by other people. For
> example, if I have:
>
> (defstruct foo
> (a nil)
> (b nil))
>
> (setf bar (make-foo :a 'x :b 'y))
>
> I need to have something like:
>
> (slot-names bar)
>
> => (a b) ; return value
to which Barry replied:
> There's no standard way.
It's true that there is no way *built in* to the standard to do
this, but it is possible to do (something very close to) this
in a way that *conforms* to the standard, e.g.:
(defmacro defstruct-preserving-slot-metadata (name &rest slots)
(setf (get (extract-name-from-name-spec name) 'struct-slot-names)
(extract-slot-names-from-slot-specs slots))
`(defstruct ,name ,@slots))
(defun slot-names (struct-name)
(get struct-name 'struct-slot-names))
Erann Gat
···@jpl.nasa.gov
From: Jon S Anthony
Subject: Re: Get slot names from struct instance?
Date:
Message-ID: <390E1224.7235@synquiry.com>
Erann Gat wrote:
>
> In article <············@news3.bu.edu>, Robert Gaimari
> <········@cs.bu.edu> wrote:
>
> > I need to be able to find the slot names of a structure from an instance
> > of it. I don't have access to changing the structure definitions to
> > accommodate this, as there is a lot of code written by other people. For
> > example, if I have:
> >
> > (defstruct foo
> > (a nil)
> > (b nil))
> >
> > (setf bar (make-foo :a 'x :b 'y))
> >
> > I need to have something like:
> >
> > (slot-names bar)
> >
> > => (a b) ; return value
>
> to which Barry replied:
>
> > There's no standard way.
>
> It's true that there is no way *built in* to the standard to do
If you don't have the MOP. But ACL has the MOP. So, he can just
use:
(class-slot-names bar)
/Jon
--
Jon Anthony
Synquiry Technologies, Ltd. Belmont, MA 02478, 617.484.3383
"Nightmares - Ha! The way my life's been going lately,
Who'd notice?" -- Londo Mollari
IIRC, all the information in this thread is in the FAQ, which I referred to
in my original response.
--
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
Robert Gaimari <········@cs.bu.edu> writes:
> Hi,
>
> I'm having trouble finding the answer to this question and hope someone
> can help me out. Chances are there is an easy answer, and I'm just not
> seeing it.
>
> I need to be able to find the slot names of a structure from an instance
> of it.
AFAIK there's no easy portable way to do it in Common Lisp.
If Elisp will do, you could borrow my tehom-cl.el packages, which does
that (tehom-get-struct-slots). It's on my web page, it's also part of
http://world.std.com/~tob/my-code/rtest-latest.tar.gz
Before you ask about porting it, it relies on specific details of `cl'
that aren't portable to Lisp in general.
--
Tom Breton, http://world.std.com/~tob
Not using "gh" since 1997. http://world.std.com/~tob/ugh-free.html
Rethink some Lisp features, http://world.std.com/~tob/rethink-lisp/index.html
Some vocal people in cll make frequent, hasty personal attacks, but if
you killfile them cll becomes usable.
From: Robert Gaimari
Subject: Re: Get slot names from struct instance?
Date:
Message-ID: <8en6jc$jjk$1@news3.bu.edu>
Thanks for all of your help. With your guidance, I was able to find the
answer. http://www.franz.com/support/docs/5.0.1/doc/cl/mop/concepts.html
has a good description of what to do. If anyone is interested, I can
send/post the exact code I finally used.
To the gentleman who referred me to the FAQ: I apologize for not consulting
it, but I did look for it. I checked two servers, both with posts dating
back to April 12, and could not find one posted. I also searched
Dejanews, using both "faq" and "frequently asked questions", but could
find none. I am not a regular reader of this group, and so must have
missed its last posting. Perhaps you could post a pointer to it? I, for
one, would appreciate it.
Thanks again.
- Bob Gaimari
In article <············@news3.bu.edu>,
Robert Gaimari <········@cs.bu.edu> wrote:
>Thanks for all of your help. With your guidance, I was able to find the
>answer. http://www.franz.com/support/docs/5.0.1/doc/cl/mop/concepts.html
>has a good description of what to do. If anyone is interested, I can
>send/post the exact code I finally used.
>
>To the gentleman who referred me to the FAQ: I apologize for not consulting
>it, but I did look for it. I checked two servers, both with posts dating
>back to April 12, and could not find one posted. I also searched
>Dejanews, using both "faq" and "frequently asked questions", but could
>find none. I am not a regular reader of this group, and so must have
>missed its last posting. Perhaps you could post a pointer to it? I, for
>one, would appreciate it.
I don't think it's posted to the group these days, but it's available on
the web at
<http://www.cs.cmu.edu/Web/Groups/AI/html/faqs/lang/lisp/top.html>. You
could have found it through the Internet FAQ Consortium, www.faqs.org
(that's how I found it).
This particular question is answered at
<http://www.cs.cmu.edu/Web/Groups/AI/html/faqs/lang/lisp/part5/faq-doc-3.html>
--
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
In article <·················@burlma1-snr2>, Barry Margolin
<······@genuity.net> wrote:
> You
> could have found it through the Internet FAQ Consortium, www.faqs.org
> (that's how I found it).
Search engines are also really good at this sort of thing. For example,
try <a href=http://www.google.com/search?q=lisp+faq+structure+slot+by+name&
num=10& meta=hl%3Den%26lr%3D> a google search on "lisp faq structure slot".</a>
E.