[Prev][Next][Index][Thread]

Re: how to make a vector of constants?



On Tue, 7 Mar 2000 dauclair@hotmail.com wrote:
> I'm building this system:
> 
> define constant $jack = 11;
> define constant $queen = 12;
> define constant $king = 13;
> define constant $ace = 14;
> 
> and I want to do this:
> 
> let faces = #[ $jack, $queen, $king, $ace ];
> 
> when I try to compile I get [FD]:
> 
> Unexpected token: "$jack".

Yep, the syntax of vector literals only allows literals inside.  Also, I
believe the objects created by vector literals are not guaranteed to be
mutable (or indeed may be guaranteed to be mutable, I can't remember).
The details are in the DRM, uh, somewhere ;-)

> Is there a simpler/cleaner way than:
> 
> let faces = make(<vector>,size: 4);
> faces[0] := $jack;
> faces[1] := $queen;
> faces[2] := $king;
> faces[3] := $ace;

Yes, and this answers your question under a separate thread about the
utility of the vector function:

  let faces = vector($jack, $queen, $king, $ace);

(You may want to tighten up the type of "faces" to <simple-object-vector>
later on.)  Using "#[]" versus "vector()" is a bit of a Dylan gotcha and
should probably be in a FAQ somewhere.  Basically, only use "#[]" if
everything inside is a *literal* (not just a constant); otherwise use
"vector()". 

HTH,
Hugh





Follow-Ups: References: