From: Robert Folland
Subject: Structure slot-init accessing previous slot
Date: 
Message-ID: <87665xb0fs.fsf@dagros.circinus.no>
I have a little problem which I couldn't find an answer to by looking
in the Hyperspec:

In a defstruct I'd like in a slot-initialization to access a
previously initialized slot of the same structure. Something analogous
to let* I guess. Is this possible?

Example:

(defstruct test
  (a-slot 10)
  (b-slot (* 3 a-slot)))
       
-- 
Robert Folland
······@circinus.no

From: Tim Bradshaw
Subject: Re: Structure slot-init accessing previous slot
Date: 
Message-ID: <ey3r8ol9gbs.fsf@cley.com>
* Robert Folland wrote:

> (defstruct test
>   (a-slot 10)
>   (b-slot (* 3 a-slot)))
       
No, I don't think it is.  Use classes if you want that kind of thing
and define methods on INITIALIZE-INSTANCE to do the work.

--tim
From: Peder O. Klingenberg
Subject: Re: Structure slot-init accessing previous slot
Date: 
Message-ID: <ujubsfpyrdn.fsf@false.linpro.no>
Robert Folland <······@circinus.no> writes:

> In a defstruct I'd like in a slot-initialization to access a
> previously initialized slot of the same structure. Something analogous
> to let* I guess. Is this possible?

Not using the slot-initforms, I think.  You need to build your own
constructor.  In CLOS, you could specialize on initialize-instance,
but using structs, I think you need to build your own function, which
explicitly calls the real constructor.

I guess a way to make this less visible to users of the struct is to
have a (defstruct (mystruct (constructor real-make-mystruct)) a b c)
and then define your own (defun make-mystruct (&key a b c) ...), which
can have complex knowledge of dependencies and default values for a b
and c.

...Peder...
-- 
Cogito ergo panta rei.
From: Lieven Marchand
Subject: Re: Structure slot-init accessing previous slot
Date: 
Message-ID: <m3d705dnj6.fsf@localhost.localdomain>
Robert Folland <······@circinus.no> writes:

> I have a little problem which I couldn't find an answer to by looking
> in the Hyperspec:
> 
> In a defstruct I'd like in a slot-initialization to access a
> previously initialized slot of the same structure. Something analogous
> to let* I guess. Is this possible?
> 
> Example:
> 
> (defstruct test
>   (a-slot 10)
>   (b-slot (* 3 a-slot)))

If you really want to do this with structures, you need a
boa-constructor. (Too bad Kent isn't reading at the moment. I'd really
like to know who was responsible for this pun.) But I would advise you
to use classes for this behaviour.

CL-USER 8 > (defstruct (test
                        (:constructor create-test
                        (a-slot &optional (b-slot (* 3 a-slot)))))
              a-slot b-slot)
TEST

CL-USER 9 > (create-test 1 2)
#S(TEST A-SLOT 1 B-SLOT 2)

CL-USER 10 > (create-test 1)
#S(TEST A-SLOT 1 B-SLOT 3)

-- 
Lieven Marchand <···@wyrd.be>
She says, "Honey, you're a Bastard of great proportion."
He says, "Darling, I plead guilty to that sin."
Cowboy Junkies -- A few simple words
From: Robert Folland
Subject: Re: Structure slot-init accessing previous slot
Date: 
Message-ID: <87wuycap3n.fsf@dagros.circinus.no>
Lieven Marchand <···@wyrd.be> writes:

> > (defstruct test
> >   (a-slot 10)
> >   (b-slot (* 3 a-slot)))
> 
> If you really want to do this with structures, you need a
> boa-constructor. (Too bad Kent isn't reading at the moment. I'd really
> like to know who was responsible for this pun.) But I would advise you
> to use classes for this behaviour.
> 
> CL-USER 8 > (defstruct (test
>                         (:constructor create-test
>                         (a-slot &optional (b-slot (* 3 a-slot)))))
>               a-slot b-slot)
> TEST
> 
> CL-USER 9 > (create-test 1 2)
> #S(TEST A-SLOT 1 B-SLOT 2)
> 
> CL-USER 10 > (create-test 1)
> #S(TEST A-SLOT 1 B-SLOT 3)

Thank you (to all who replied). I'll use the specific constructor as
in your example to do it for the moment. But as you suggest, I'll
switch to using CLOS, I'll do that when my code is a little more
"settled". I'll be playing around and changing things quite a bit for
a while I think. "Exploratory computing" is perhaps a nicer way of
putting it :-)

-- 
Robert Folland
······@circinus.no
From: Peder O. Klingenberg
Subject: Re: Structure slot-init accessing previous slot
Date: 
Message-ID: <ujud7045dr6.fsf@false.linpro.no>
Robert Folland <······@circinus.no> writes:

> I'll be playing around and changing things quite a bit for a while I
> think. "Exploratory computing" is perhaps a nicer way of putting it

In that case, I think you'd benefit from switching to CLOS sooner
rather than later.  At least in my experience with CMUCL, redefining
structs is a hassle.  Maybe it works in your imlpementation, but it's
not standard, in fact the hyperspec says the consequences are
undefined.  Redefining CLOS classes is of course a breeze.  When I do
exploratory programming, I find it's easier to start with CLOS classes
and switch to structs if and when the need arises and the design has
settled.

...Peder...
-- 
Cogito ergo panta rei.
From: Robert Folland
Subject: Re: Structure slot-init accessing previous slot
Date: 
Message-ID: <87vgdod3nm.fsf@dagros.circinus.no>
·····@news.klingenberg.no (Peder O. Klingenberg) writes:

> In that case, I think you'd benefit from switching to CLOS sooner
> rather than later.  At least in my experience with CMUCL, redefining
> structs is a hassle.  Maybe it works in your imlpementation, but it's
> not standard, in fact the hyperspec says the consequences are
> undefined.  Redefining CLOS classes is of course a breeze.  When I do
> exploratory programming, I find it's easier to start with CLOS classes
> and switch to structs if and when the need arises and the design has
> settled.

You may be right. I've been looking at CLOS a little now, and I think
I'll give it a try, even if I'm still pretty new at CL.

CLOS seems much more flexible than OO-programming in other
languages. I've done a tiny bit of C++ and a bit of Java, and there I
found that I could "get stuck" with a bad design too early. But with
CLOS I think a lot is left more "open", and more suited for
exploratory programming, as you say. Thanks.

-- 
Robert Folland
······@circinus.no