There has been discussion of allowing a "default" value for array cells -- that is, one aside from C<undef> or whatever the type-specific default is. Questions, in order of increased evilness:

1) What's the final decided syntax? Two possibilities:

my @a is Array( default => 'foo' ); # attrib?
my @a is default('foo'); # property?


2) Assume the default value is a simple value, e.g. 'foo'.

my @a is Array( default => 'foo' );
@a[5] = 'bar';

@a[4]; # 'foo'
@a[5]; # 'bar'
@a[6]; # 'foo'

@a[-1]; # 'bar' *NOTE!*
@a[-3]; # 'foo'
@a[-10]; # 'foo'

Correct?


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'.


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?

@a[0] = undef;
@a[0]; # 0, or 5?

STRAWMAN ANSWER: 5, in both cases. So don't do that unless you mean it.


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.


3a) NOTE that closure-based defaults effectively render the array infinite. Therefore -- If the requested index is negative, what happens?

@a[-5];

STRAWMAN ANSWER: The closure just gets a negative number as the requested index.
It's up to you to make it work, if you want it to.


3b) Does an "infinite" array still get an exception thrown when trying to access an infinite [Inf] or [-Inf] index?

STRAWMAN ANSWER: Yes, it does.


MikeL

Reply via email to