From: Tim Josling
Subject: Re:  DEFSTRUCT and EXPORT
Date: 
Message-ID: <477f66d1-3732-4b39-9453-17330fbd0bd6@s8g2000prg.googlegroups.com>
>Newsgroups: comp.lang.lisp
>From: ยทยทยทยท@litp.UUCP (Pierre JOUVELOT)
>Date: 15 Nov 88 19:30:32 GMT
>Local: Wed, Nov 16 1988 6:30 am
>Subject: DEFSTRUCT and EXPORT

> 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)))

From: Maciej Katafiasz
Subject: Re:  DEFSTRUCT and EXPORT
Date: 
Message-ID: <fk8fs7$era$1@news.net.uni-c.dk>
Den Tue, 18 Dec 2007 01:31:28 -0800 skrev Tim Josling:

> (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
From: tim Josling
Subject: Re:  DEFSTRUCT and EXPORT
Date: 
Message-ID: <13mg7m0ff2cee1b@corp.supernews.com>
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
From: tim Josling
Subject: Re:  DEFSTRUCT and EXPORT
Date: 
Message-ID: <13mg8uqihos074@corp.supernews.com>
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...

(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)))
From: tim Josling
Subject: Re:  DEFSTRUCT and EXPORT
Date: 
Message-ID: <13mg9hnp1c36rdd@corp.supernews.com>
On Tue, 18 Dec 2007 19:47:38 +0000, tim Josling wrote:

> 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)))
From: Maciej Katafiasz
Subject: Re:  DEFSTRUCT and EXPORT
Date: 
Message-ID: <fk9bdr$hld$1@news.net.uni-c.dk>
Den Tue, 18 Dec 2007 19:57:43 +0000 skrev tim Josling:

> 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