Re: structures and arrays : one conses the other doesn't
Tim Bradshaw <tfb@cley.com> writes:
> * Barry Wilkes wrote:
>
> > It would basically be a lot 'nicer' to be able to use structures in this way
> > (for some of my code), rather than arrays.
>
> I think, as Nick says, the answer is that the special unboxed float
> support is only implemented for arrays.
>
> However it turns out that you can do this, which I think is not
> completely portable, but seems to work on LWW:
>
> (defstruct (foo (:type (vector single-float)))
> fish
> fob)
Thanks again Tim, this is exactly what I want. The following works perfectly :
(defstruct (foo (:type (vector single-float)))
fish
fob)
(deftype foo () `(simple-array single-float (2)))
(defun testing (a b c)
(declare (type single-float b c d)
(type foo a)
(optimize (speed 3) (safety 0) #+LispWorks(float 0) (debug 0)))
(setf (foo-fish a) (+ b c))
a)
and yes, the above function does not cons. I will be using this quite a bit -
maybe not as nice as being able to specify different types at a slot level,
but very useful. I did not realise that one could specify the representation
to be used for a structure - this bit of the Hyperspec was new to me.
To be honest, when I was (still am, of course) initially learning CL, I
concentrated on defclass rather than defstruct. This was probably a good thing
in general, but I now am aware that structures are very useful in their own
right, and in paticular where performance is critical.
Barry.
--
If in the last few years you haven't discarded a major opinion or
acquired a new one, check your pulse. You may be dead.
-- Gelett Burgess (1866-1951)