Michael Lazzaro wrote:
It's certainly a legitimate question. (I'm not sure that @a[undef] == @a[0] is the correct behavior, going forward. It certainly can be error-prone.)1) Edge cases in array indexing:@a[ undef ] # undef, or 1? @a['foo'] # undef, or 1?These should generate warnings, at least. I don't know whether undef or 1 is more correct.
We need an expert decision here, please.
I'd expect that C<undef> converts to zero (with an evitable warning). I would expect that non-numeric strings like 'foo' convert to C<undef> and thence on to zero (again with an evitable warning). Unless C<use strict 'conversions'> is in effect, of course. Note that converting to and from C<undef> in numeric contexts is much less troublesome in Perl 6, because the C<//> operator makes it trivial to catch bad cases and handle them appropriately: @a[$str//0] // silently convert to zero @a[$str//die] // silently convert to exception
Actually, I would expect it to throw an exception. Using something that isWhat about @a[NaN]? Just another warning probably.
explicitly and deliberately *not* a number [*], in a context that expects a number, almost certainly points to a serious problem in the code.
Damian
[*] ...as opposed to 'foo', which is merely implicitly and accidentally not a number ;-)