Juerd wrote:
> Ingo Blechschmidt skribis 2005-09-09 11:59 (+0000):
>> > > > \(@array,) is [ @array ], NOT map { \$_ } @array
>> > > I'm not sure of the []s, remember &postcirumfix:<[ ]> creates
>> > > *new* containers:
>> > That was the point.
>> > >     [EMAIL PROTECTED] = $bar;
>> > >     (@array,)[0] = $bar;
>> > AFAIK, these are the same thing, because the left side of [0] is in
>> > Array context, which is a scalar context, in which comma creates a
>> > new anonymous array ref.
>> To prevent misconceptions: You think that both
>>     [EMAIL PROTECTED]  = $bar;  # and
>>     (@array,)[0] = $bar;
>> change @array[0] to $bar, right?
> No, neither does.
> [EMAIL PROTECTED] = $bar fills a new array with the elements of @array, and
> [then
> overwrites the first. The array is then discarded.


> (@array,)[0] = $bar does the same, IIRC, because the LHS of .[0] is
> object and thus scalar context, possibly specifically Array context,
> and the comma operator then behaves as if it has [] around it: it
> creates an anonymous array. Again, it's just the elements of @array
> that are used.

Ah! I think I got your point now! :)

I agree that the comma operator creates an anonymous array, but I do not
agree that it behaves as if it has [] around it.

Creating an anonymous array does not require creating new containers --
if I've understood things correctly, then the comma operator creates
arrayrefs (in appropriate contexts), but does *not* create new
containers. By contrast, the [] operator always creates arrayrefs
holding new containers.

(The comma operator *may not* create new container containers, because

    ($foo, $bar)[1] = $baz;

should change $bar to $baz (you reminded me of this property of
&infix:<,> in

> Now, (@array)[0] = $bar does assign to @array[0].

Of course, as in this case neither the comma operator nor the []
operator is used, the () are only used for grouping.

> I think the comma operator in scalar context should not create arrays,
> because that is wildly confusing for most people, and a dangerous trap
> even for those who do grok it. We already have [] for creating
> anonymous arrays, and here a second WTDI isn't buying us anything.

I'd probably agree *if* &cirumfix:<[ ]> and &infix:<,> really did the
same thing (creating arrays containing new containers), but, IIUC,
&infix:<,> does not create new containers while &postcirumfix:<[ ]>

>>     (my $arrayref = [1,2,3])[1] = 42;
> [1, 42, 3]. With () for grouping, not lists, and [] working on
> references as well as the original, you're assigning to $arrayref[1],
> not the nonexistent second element of the nonexistent list.

I agree.

>>     sub *postcircumfix:<[ ]> ([EMAIL PROTECTED]) {
> Do postfix list operators exist in Perl 6? AFAIK, the only thing that
> can create a list is list context, and I'm very unsure how anything
> that can handle both a list and an item can be postfix.

D'oh, I meant &circumfix:<[ ]>, not &postcircumfix:<[ ]>.

&circumfix:<[ ]> is the operator used in [1,2,3], &postcirumfix:<[ ]> is
the operator used in @array[$index].


Reply via email to