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?
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/
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)))
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__
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/
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