From: zoav1602
Subject: slot names in slot-initform
Date: 
Message-ID: <2b05ffa4-8580-4092-a685-c5798b7ffaf7@y21g2000hsf.googlegroups.com>
Hello,
My sbcl fails to construct structure in the following example:

(defstruct s1 (size 100) (d1 size))

(make-s1)
  ==> undefined variable size

In fact I saw this usage of slot-name in slit-initform in a trusted
lisp program wich I fail to load into sbcl. Can someone help?

From: Pascal Costanza
Subject: Re: slot names in slot-initform
Date: 
Message-ID: <6l35veFae9iqU1@mid.individual.net>
zoav1602 wrote:
> Hello,
> My sbcl fails to construct structure in the following example:
> 
> (defstruct s1 (size 100) (d1 size))
> 
> (make-s1)
>   ==> undefined variable size
> 
> In fact I saw this usage of slot-name in slit-initform in a trusted
> lisp program wich I fail to load into sbcl. Can someone help?

You want to use the :constructor option of defstruct to have better 
control over initialization of slots.


Pascal

-- 
Lisp50: http://www.lisp50.org

My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: zoav1602
Subject: Re: slot names in slot-initform
Date: 
Message-ID: <8bafaca2-44a2-4aa7-961b-4b6d4d466b0d@c22g2000prc.googlegroups.com>
On Oct 8, 11:31 am, Pascal Costanza <····@p-cos.net> wrote:
> zoav1602 wrote:
> > Hello,
> > My sbcl fails to construct structure in the following example:
>
> > (defstruct s1 (size 100) (d1 size))
>
> > (make-s1)
> >   ==> undefined variable size
>
> > In fact I saw this usage of slot-name in slit-initform in a trusted
> > lisp program wich I fail to load into sbcl. Can someone help?
>
> You want to use the :constructor option of defstruct to have better
> control over initialization of slots.
>
> Pascal
>
> --
> Lisp50:http://www.lisp50.org
>
> My website:http://p-cos.net
> Common Lisp Document Repository:http://cdr.eurolisp.org
> Closer to MOP & ContextL:http://common-lisp.net/project/closer/

The initial idea was that we can write both (make-s1) and (make-s1
12334) which seems impossible with ise of :constructor. In fact, the
original code looks like
(defstruct s1 (size 100) (d1 (make-array size)))
From: Pascal J. Bourguignon
Subject: Re: slot names in slot-initform
Date: 
Message-ID: <7c8wsz4a7t.fsf@pbourguignon.anevia.com>
zoav1602 <········@gmail.com> writes:
> The initial idea was that we can write both (make-s1) and (make-s1
> 12334) which seems impossible with ise of :constructor. In fact, the
> original code looks like
> (defstruct s1 (size 100) (d1 (make-array size)))

(defstruct (s1 (:constructor %make-s1)) size d1)

(defun make-s1 (&key (size 100) d1) ; most often, d1 is private so you don't even have it here...
   (%make-s1 :size size :d1 (or d1 (make-array size))))

-- 
__Pascal Bourguignon__
From: Pascal Costanza
Subject: Re: slot names in slot-initform
Date: 
Message-ID: <6l3o7jFag71aU1@mid.individual.net>
zoav1602 wrote:
> On Oct 8, 11:31 am, Pascal Costanza <····@p-cos.net> wrote:
>> zoav1602 wrote:
>>> Hello,
>>> My sbcl fails to construct structure in the following example:
>>> (defstruct s1 (size 100) (d1 size))
>>> (make-s1)
>>>   ==> undefined variable size
>>> In fact I saw this usage of slot-name in slit-initform in a trusted
>>> lisp program wich I fail to load into sbcl. Can someone help?
 >>
>> You want to use the :constructor option of defstruct to have better
>> control over initialization of slots.
>>
> The initial idea was that we can write both (make-s1) and (make-s1
> 12334) which seems impossible with ise of :constructor. In fact, the
> original code looks like
> (defstruct s1 (size 100) (d1 (make-array size)))

(defstruct (s1 (:constructor make-s1
                  (&key (size 100) &aux (dl (make-array size)))))
   size dl)

&aux introduces local variables, just like LET in function bodies. You 
cannot pass arguments to &aux parameters, so they are usually quite 
redundant, but in the case of defstruct, they are actually quite handy.

Struct slots are initialized by constructor lambda lists based on the 
names of the parameters - that's why size and dl are initialized 
correctly here. (Should be, I haven't tested the code above.)

You can use other lambda list keywords in constructor lambda lists, for 
example, &optional instead of &key, etc.

I hope this helps.


Pascal


P.S.: Kenny, this would be another excellent opportunity to say 
something about RDF, Cells, JavaScript, AJAX, Arc, or any other 
arbitrary topic you're currently excited about.

-- 
Lisp50: http://www.lisp50.org

My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Kenny
Subject: Re: slot names in slot-initform
Date: 
Message-ID: <48ecc423$0$4883$607ed4bc@cv.net>
Pascal Costanza wrote:
> zoav1602 wrote:
> 
>> On Oct 8, 11:31 am, Pascal Costanza <····@p-cos.net> wrote:
>>
>>> zoav1602 wrote:
>>>
>>>> Hello,
>>>> My sbcl fails to construct structure in the following example:
>>>> (defstruct s1 (size 100) (d1 size))
>>>> (make-s1)
>>>>   ==> undefined variable size
>>>> In fact I saw this usage of slot-name in slit-initform in a trusted
>>>> lisp program wich I fail to load into sbcl. Can someone help?
> 
>  >>
> 
>>> You want to use the :constructor option of defstruct to have better
>>> control over initialization of slots.
>>>
>> The initial idea was that we can write both (make-s1) and (make-s1
>> 12334) which seems impossible with ise of :constructor. In fact, the
>> original code looks like
>> (defstruct s1 (size 100) (d1 (make-array size)))
> 
> 
> (defstruct (s1 (:constructor make-s1
>                  (&key (size 100) &aux (dl (make-array size)))))
>   size dl)
> 
> &aux introduces local variables, just like LET in function bodies.

Wrong. It's just like LET*.

To the OP, one of my favorite things about Cells is being able to 
reference the nascent instance in initforms and initargs.

> P.S.: Kenny, this would be another excellent opportunity to say 
> something about RDF, Cells, JavaScript, AJAX, Arc, or any other 
> arbitrary topic you're currently excited about.

You refer of course to SymbolicWeb. Or possibly Rachel Maddow, the 
rising left-wing star on MSNBC.

Meanwhile, are you sure you want to advertise so loudly your lack of 
intellectual diversity? You have all the variety of a well-functioning 
metronome, and your attempt at provocation mirrors nicely the contrast 
in our openness to new technology.

kt