> Hi,
>I'm trying to define a bunch of DEFSTRUCT in a given package (let's say :FOO).
>I'm having some difficulties exporting all the functions (creation, access,
>and SETF methods) that are created by DEFSTRUCT.
>The current trick is to perform a DO-SYMBOLS on :FOO and to EXPORT all the
>symbols that are :INTERNAL in it (there is nothing else in the :FOO
>package). That sort of works but, first this is ugly and, second I still
>have to specify the :FOO package when I want to use SETF to modify
>a member in a structure (at least in my Allegro CommonLISP from Franz).
>Am I plainly wrong and there is a better way, or is this a shortcoming
>of DEFSTRUCT ?
>Thanks in advance,
>Pierre
--
> Pierre Jouvelot
> Centre d'Automatique et Informatique
> Ecole des Mines
> ...
This might be a bit late being 19 years after your question, but there
is an elaborate replacement for defstruct which does what you want
(assuming it works). See http://common-lisp.net/project/defclass-star/
(over 200 lines of code just for the main function)
Here is a simplistic solution that covers only simple cases (no
overrides of names etc):
(defmacro simple-defstruct-and-export (structure specials &rest
members)
"Define a structure STRUCT with members MEMBERS and export the
standard functions created. SPECIALS is a list of extra parameters
eg ((:print-function pf)). Note double parentheses."
(append
(list 'progn
(append (list 'defstruct (append (list structure) specials))
members)
(list 'export (list 'quote (intern (concatenate 'string
"MAKE-" (symbol-name structure)))))
(list 'export (list 'quote (intern (concatenate 'string
"COPY-" (symbol-name structure))))))
(mapcar
#'(lambda (member)
(list 'export
(list 'quote (intern (concatenate 'string (symbol-name
structure) "-" (symbol-name member))))))
members)))
> (defmacro simple-defstruct-and-export (structure specials &rest members)
> "Define a structure STRUCT with members MEMBERS and export the
> standard functions created. SPECIALS is a list of extra parameters eg
> ((:print-function pf)). Note double parentheses."
> (append
> (list 'progn
> (append (list 'defstruct (append (list structure) specials))
> members)
> (list 'export (list 'quote (intern (concatenate 'string
> "MAKE-" (symbol-name structure)))))
> (list 'export (list 'quote (intern (concatenate 'string
> "COPY-" (symbol-name structure))))))
> (mapcar
> #'(lambda (member)
> (list 'export
> (list 'quote (intern (concatenate 'string (symbol-name
> structure) "-" (symbol-name member))))))
> members)))
Why on earth are you not using backquote?
Cheers,
Maciej
> Why on earth are you not using backquote?
>
> Cheers,
> Maciej
Originally I did but I have not yet mastered the rules for nested
backquotes and overrides and gave up. Three levels in this case. You are
welcome to show us how it's done.
Tim Josling
I have gotten this far...
(defmacro simple-defstruct-and-export (structure specials &rest members)
"Define a structure STRUCT with members MEMBERS and export the
standard functions created. SPECIALS is a list of extra parameters
eg ((:print-function pf)). Note double parentheses."
(append
`(progn
,(append `(defstruct ,(append (list structure) specials)) members)
,`(export ,(list 'quote (intern (concatenate 'string "MAKE-" (symbol-name structure)))))
,`(export ,(list 'quote (intern (concatenate 'string "COPY-" (symbol-name structure))))))
(mapcar
#'(lambda (member)
`(export
,(list 'quote (intern (concatenate 'string (symbol-name structure) "-" (symbol-name member))))))
members)))
> On Tue, 18 Dec 2007 19:25:52 +0000, tim Josling wrote:
>
>> On Tue, 18 Dec 2007 12:53:27 +0000, Maciej Katafiasz wrote:
>>
>>
>>> Why on earth are you not using backquote?
>>>
>>> Cheers,
>>> Maciej
>>
>> Originally I did but I have not yet mastered the rules for nested
>> backquotes and overrides and gave up. Three levels in this case. You are
>> welcome to show us how it's done.
>>
>> Tim Josling
>
> I have gotten this far...
> ...
Now, "list" does not appear so I have probably used backquote maximally. I
hope I never have to debug this. Are there any further suggestions?
(defmacro simple-defstruct-and-export (structure specials &rest members)
"Define a structure STRUCT with members MEMBERS and export the
standard functions created. SPECIALS is a list of extra parameters eg
((:print-function pf)). Note double parentheses."
(append
`(progn
,(append `(defstruct ,(append `(,structure) specials)) members)
,`(export ,`(quote ,(intern (concatenate 'string "MAKE-" (symbol-name structure)))))
,`(export ,`(quote ,(intern (concatenate 'string "COPY-" (symbol-name structure))))))
(mapcar
#'(lambda (member)
`(export ,`(quote ,(intern (concatenate 'string(symbol-namestructure) "-" (symbol-name member))))))
members)))
> Now, "list" does not appear so I have probably used backquote maximally.
> I hope I never have to debug this. Are there any further suggestions?
>
> (defmacro simple-defstruct-and-export (structure specials &rest
> members)
> "Define a structure STRUCT with members MEMBERS and export the
> standard functions created. SPECIALS is a list of extra parameters eg
> ((:print-function pf)). Note double parentheses."
> (append
> `(progn
> ,(append `(defstruct ,(append `(,structure) specials)) members)
> ,`(export ,`(quote ,(intern (concatenate 'string "MAKE-"
> (symbol-name structure))))) ,`(export ,`(quote ,(intern
> (concatenate 'string "COPY-" (symbol-name structure))))))
> (mapcar
> #'(lambda (member)
> `(export ,`(quote ,(intern (concatenate
> 'string(symbol-namestructure) "-" (symbol-name member))))))
> members)))
That looks much nicer. Well done :)
Cheers,
Maciej