From: masti
Subject: Question about structures
Date: 
Message-ID: <cpnm6u$1j7$1@atlantis.news.tpi.pl>
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?

From: Wade Humeniuk
Subject: Re: Question about structures
Date: 
Message-ID: <qbJvd.78464$6f6.12883@edtnps89>
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
From: Kenny Tilton
Subject: Re: Question about structures
Date: 
Message-ID: <rgJvd.24696$Yh2.11201609@twister.nyc.rr.com>
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
From: Jeff
Subject: Re: Question about structures
Date: 
Message-ID: <woKvd.499866$wV.300652@attbi_s54>
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
From: Barry Margolin
Subject: Re: Question about structures
Date: 
Message-ID: <barmar-D0D44E.00505615122004@comcast.dca.giganews.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 ***