On Thu, 2002-05-16 at 12:36, Miko O'Sullivan wrote:

> I submit for consideration the idea that if an array doesn't always have a
> defined length then it ceases to be that incredibly handy construct that we
> currently call "array".  If arrays can answer "I dunno" when asked how long
> they are, that's taking away a useful feature, not adding one. If we dumb
> down arrays we're going to have to spend a lot of time smarting up our code.
> Suddenly every time you want to use an array in scalar or numeric context
> you first have to check if it has a defined length. That doesn't appeal to
> my sense of laziness, and it triggers my sense of impatience.

I don't see that that's the case. I do see that in some cases, you will
get a "useless array" in the sense that your "dumb" code:

    my @ary is Too::Smart::Array = (1,2,3); # uh... is that how tie will work?
    if @ary.length { ... do stuff ... }

So, this code will work correctly as long as @ary has a valid numeric
value, but if it doesn't your code will treat that has having zero
elements. That seems roughly correct to me. The only "correcter" thing
would be something like:

    my @ary is Too::Smart::Array = (1,2,3);
    if my $len = @ary.length {
        ...do stuff...
    } else unless defined $len {
        die "$0: Yes, we have no bananas: $!";
    }

Why would you want to do this? Let's say that you're tying an array to a
very large, possibly infinite set of data, or a source of data that is
slow. You might not be able to REASONABLY get a length, so you return
undef. In your documentation, you advise users not to take the length,
but just dive right in and fetch the element you want, e.g.:

    my $pi2k = @pi_digits[2000];

Users who do not understand this will get odd results, true, but they
get odd results that result in a reasonable default, IMHO.


Reply via email to