develooper Front page | perl.perl6.language | Postings from January 2003

Re: More Array Behaviors

From:
Damian Conway
Date:
January 27, 2003 14:50
Subject:
Re: More Array Behaviors
Message ID:
3E35B7AB.9010802@conway.org
Michael Lazzaro wrote:

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



>> What about @a[NaN]?  Just another warning probably.

Actually, I would expect it to throw an exception. Using something that is
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 ;-)




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About