I think this debate is easier if you think of defaults as overriding and auto-vivification method on a container.
On Tue, 2003-01-28 at 14:47, Paul Johnson wrote: > Michael Lazzaro said: > > 2a) When a cell is explicitly re-undefined, does the default value take > > effect? > > > > my @a is Array( default => 'foo' ) = (1,2,3); > > > > @a[1] = undef; > > @a[1]; # undef, or 'foo'? > > > > STRAWMAN ANSWER: 'foo'. > > Seems right to me. Anything else would be very confusing, I think. This would be very confusing to me. In fact, it seems WRONG to me unless we're saying that such arrays can simply never hold an undefined value... which again seems wrong. my @a is Array ( default => 'foo' ); @a[1] = undef; Should yield the following, I would think: ('foo', undef, 'foo' x Inf) Though, obviously there's no such thing as @a[2], but if you reference it, that's how it would auto-vivify. If that's not the case, I need to get my head around why, since Perl *does* distinguish between defined and exists. > > 2b) Primitive-typed arrays: Given the behavior of (2a), and the fact > > that primitive-typed arrays can't store undef, what happens here? > > > > my int @a is Array( default => 5 ); > > > > @a[0] = 0; > > @a[0]; # 0, or 5? > > 0. Being unable to store 0 would seem to be a major limitation. But of course. > > @a[0] = undef; > > @a[0]; # 0, or 5? > > An exception or 5. The first assignment would attempt to convert undef to type int. That's either an exception or zero, no? The "is Array" is a constraint on the container, and should not be entering into the conversion at hand until you try to STORE the result. > Maybe undefining an element could always set it to the default value. > > > 3) Can the default value be a closure, based on index location? > > > > my @a is Array( default => { $_ ** 2 }); > > > > STRAWMAN ANSWER: Yes, because it's cool. > > No, because it's unnecessary. You can always do > > my $value = @a[$x] //= $x ** 2; Again, different. You're looking for something like C<ifexists=>, not C<//=> -- Aaron Sherman <[EMAIL PROTECTED]> This message (c) 2003 by Aaron Sherman, and granted to the Public Domain in 2023. Fight the DMCA and copyright extension!