I would like to initialise a structure, where some of the slots depend
on the other slots.
So what I have so far is:
(defstruct blog-entry
fname
(date (ddmmmyy (subseq :fname 1 7))))
where ddmmmyy is some function you don't need to worry about for the
purposes of this argument.
The problem is that when I do
(make-blog-entry :fname "m031021-01.txt")
I get the message
*** - :FNAME is not a sequence.
I've tried various combinations of things, all to no avail. I suppose
you could argue that I could write a function which fills in the
dependent values - but I think it would be safer to initialise them at
instantiation.
Is what I am trying to do undoable, or just plain evil? Should I be
using objects?
In article <···························@posting.google.com>,
Mark Carter <············@ukmail.com> wrote:
>I would like to initialise a structure, where some of the slots depend
>on the other slots.
>
>So what I have so far is:
>
>(defstruct blog-entry
> fname
> (date (ddmmmyy (subseq :fname 1 7))))
>
>where ddmmmyy is some function you don't need to worry about for the
>purposes of this argument.
>
>The problem is that when I do
>(make-blog-entry :fname "m031021-01.txt")
>
>I get the message
>
>*** - :FNAME is not a sequence.
If it actually worked as you expected, it should be FNAME, not :FNAME. The
latter is a keyword, which is always treated as a literal, not a variable.
>I've tried various combinations of things, all to no avail. I suppose
>you could argue that I could write a function which fills in the
>dependent values - but I think it would be safer to initialise them at
>instantiation.
>
>Is what I am trying to do undoable, or just plain evil? Should I be
>using objects?
You can do it with a BOA constructor:
(defstruct (blog-entry
(:constructor
(make-blog-entry (:key fname
(date (ddmmmyy (subseq fname 1 7)))))))
fname
date)
--
Barry Margolin, ··············@level3.com
Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: james anderson
Subject: Re: newbie: initialising structures
Date:
Message-ID: <3F954FE2.3DB7CFB7@setf.de>
Barry Margolin wrote:
>
> ...
>
> You can do it with a BOA constructor:
>
> (defstruct (blog-entry
> (:constructor
> (make-blog-entry (:key fname
> (date (ddmmmyy (subseq fname 1 7)))))))
> fname
> date)
>
?
(defstruct (blog-entry
(:constructor
make-blog-entry (&key fname
(date (ddmmmyy (subseq fname 1 7))))))
date
fname)
············@ukmail.com (Mark Carter) writes:
> I would like to initialise a structure, where some of the slots depend
> on the other slots.
>
> So what I have so far is:
>
> (defstruct blog-entry
> fname
> (date (ddmmmyy (subseq :fname 1 7))))
>
> where ddmmmyy is some function you don't need to worry about for the
> purposes of this argument.
>
> The problem is that when I do
> (make-blog-entry :fname "m031021-01.txt")
>
> I get the message
>
> *** - :FNAME is not a sequence.
>
> I've tried various combinations of things, all to no avail. I suppose
> you could argue that I could write a function which fills in the
> dependent values - but I think it would be safer to initialise them at
> instantiation.
>
> Is what I am trying to do undoable, or just plain evil? Should I be
> using objects?
I'd use object myself.
The trick is to use &AUX parameters in the constructor:
(defstruct (blog-entry
(:constructor make-blog-entry
(fname &aux (date (ddmmyy (subseq fname 1 7))))))
fname
date)