From: Andreas Hinze
Subject: Strange defstruct behavoir (?)
Date: 
Message-ID: <3D16ED99.7E3F9735@smi.de>
Hi all,
i was building a small application and run into the following problem.
The code:

  (defstruct logwindow
    ( name   "")
    ( window nil)
    ( text   nil))

  (load "CL-GTK:cl-gtk")
  (use-package "GTK")

raise an error since the package GTK exports the symbols TEXT and WINDOW.
Moving the defstruct to the end of the code sequence works fine:

  (load "CL-GTK:cl-gtk")
  (use-package "GTK")

  (defstruct logwindow
    ( name   "")
    ( window nil)
    ( text   nil)) 

Raises no error.

I found out that defstruct creates symbols WINDOW and TEXT in the USER package.
In the first code snippet the use-package checks for existing symbols and raise
the error, in the second snipped the old definitions are overwritten (?).
However, the second example still works.

Can you please explain that behavoir to me ? My particular confusion is:

1) Why creates defstruct symbols from it's structure elements ?

2) Are these symbols important ? What happens if two structures have elements
   with the same name or if i overwrite such a symbol with "(setq foo 5)" ?

3) Is there a problem when i use packages that export a symbol that has the
   same name than a structure element ? Are there side effects ?

4) When i define a structure with structure elements named like symbols imported
   from a package, what happens to the original definition of the symbol ? Is it
   still available ?

Thanks in advance
Best
AHz

From: Joe Marshall
Subject: Re: Strange defstruct behavoir (?)
Date: 
Message-ID: <CRCR8.137389$nZ3.57976@rwcrnsc53>
"Andreas Hinze" <···@smi.de> wrote in message ······················@smi.de...
> Hi all,
> i was building a small application and run into the following problem.
> The code:
>
>   (defstruct logwindow
>     ( name   "")
>     ( window nil)
>     ( text   nil))
>
>   (load "CL-GTK:cl-gtk")
>   (use-package "GTK")
>
> raise an error since the package GTK exports the symbols TEXT and WINDOW.
> Moving the defstruct to the end of the code sequence works fine:
>
>   (load "CL-GTK:cl-gtk")
>   (use-package "GTK")
>
>   (defstruct logwindow
>     ( name   "")
>     ( window nil)
>     ( text   nil))
>
> Raises no error.
>
> I found out that defstruct creates symbols WINDOW and TEXT in the USER package.

No, the reader is the one creating the symbols when the defstruct form
is read.


> In the first code snippet the use-package checks for existing symbols and raise
> the error, in the second snipped the old definitions are overwritten (?).

In the first snippet, the symbols LOGWINDOW, NAME, WINDOW, and TEXT (and
whatever symbols defstruct creates) are interned in whatever package is
current when that form is read and evaluated (the user package).

In the second snippet, you have performed a `use-package'.  Since some
symbols have been exported from the GTK package, they are now accessible
from the user package.  So when the reader sees "WINDOW" this time it
assumes you mean the symbol accessible in the user package, namely the
symbol GTK:WINDOW.

> However, the second example still works.

Yes, because despite all this defstruct doesn't do anything with the
slot name symbols other than construct accessors.

>
> Can you please explain that behavoir to me ? My particular confusion is:
>
> 1) Why creates defstruct symbols from it's structure elements ?

The reader is constructing the symbols you are referring to.
DEFSTRUCT *also* creates symbols of the form MAKE-LOGWINDOW, LOGWINDOW-NAME,
etc.

> 2) Are these symbols important ? What happens if two structures have elements
>    with the same name or if i overwrite such a symbol with "(setq foo 5)" ?

The symbols naming the slots are used only for the sake of their name.

> 3) Is there a problem when i use packages that export a symbol that has the
>    same name than a structure element ? Are there side effects ?

There should not be a problem.  The only side effect is the one you noticed.

> 4) When i define a structure with structure elements named like symbols imported
>    from a package, what happens to the original definition of the symbol ? Is it
>    still available ?

That *is* the original definition of the symbol.
From: Andreas Hinze
Subject: Re: Strange defstruct behavoir (?)
Date: 
Message-ID: <3D171EAE.4A7EF907@smi.de>
Joe Marshall wrote:
>  
> No, the reader is the one creating the symbols when the defstruct form
> is read.
> 
Sometimes i'm running straight in the wrong direction. Yes, after your
explainations and thinking some time about it the topic becomes clear.

Thanks for your help 
Sincerly
AHz