Is there a way to access a structure field having only name of that field?
Let's say I declared such a structure:
(defstruct name
field1...)
I would like to write a funciton that would access certain field which name
is secified as one of the arguments. That funcion would look like this:
(defunc my-fun (fieldname struct) ...
I's there a way to do it?
masti wrote:
> Is there a way to access a structure field having only name of that field?
> Let's say I declared such a structure:
> (defstruct name
> field1...)
>
> I would like to write a funciton that would access certain field which name
> is secified as one of the arguments. That funcion would look like this:
> (defunc my-fun (fieldname struct) ...
>
> I's there a way to do it?
>
>
Yes, though I do not think it is in the standard. This
works with CMULISP, LispWorks and Clisp.
; SLIME 2004-09-02
CL-USER> (defstruct bullet position)
BULLET
CL-USER> (setf b (make-bullet :position :in-the-heart))
Warning: Declaring B special.
#S(BULLET :POSITION :IN-THE-HEART)
CL-USER> (slot-value b 'position)
:IN-THE-HEART
CL-USER> (defun my-func (fieldname struct)
(slot-value struct fieldname))
MY-FUNC
CL-USER> (my-func 'position b)
:IN-THE-HEART
CL-USER>
Wade
masti wrote:
> Is there a way to access a structure field having only name of that field?
> Let's say I declared such a structure:
> (defstruct name
> field1...)
>
> I would like to write a funciton that would access certain field which name
> is secified as one of the arguments. That funcion would look like this:
> (defunc my-fun (fieldname struct) ...
>
> I's there a way to do it?
AllegroCL lets you use slot-value on a structure, but that is not
standard CL.
I suggest you use CLOS. You have received a Message From God(tm) that
defstruct is not what you want.
kenny
--
Cells? Cello? Celtik?: http://www.common-lisp.net/project/cells/
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
masti wrote:
> Is there a way to access a structure field having only name of that
> field? Let's say I declared such a structure:
> (defstruct name
> field1...)
>
> I would like to write a funciton that would access certain field
> which name is secified as one of the arguments. That funcion would
> look like this: (defunc my-fun (fieldname struct) ...
>
> I's there a way to do it?
Not completely, but close. You can use :conc-name to define the prefix
to the accessors. For example:
(defstruct (my-long-struct-name (:conc-name s-) x y z))
;; This will create:
;; S-X
;; S-Y
;; S-Z
Jeff M.
--
http://www.retrobyte.org
··············@gmail.com
In article <············@atlantis.news.tpi.pl>,
"masti" <·········@tenbit.pl> wrote:
> Is there a way to access a structure field having only name of that field?
This is the question that was asked so frequently over 15 years ago that
I was prompted to create the Lisp FAQ. Go read the answer there.
--
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***