Michael Lazzaro wrote:

> So, IMO, the only reasonable answer is (3)... that a list in numeric
> context returns the length.  Thus we have consistency between lists
> and arrays:
> 
>     (1,2,3) + 4       # --> (1,2,3).length + 4 --> 7  (list)
>     [1,2,3] + 4       # --> [1,2,3].length + 4 --> 7  (array ref)
> 
>     my @a = (1,2,3);  #
>     @a + 4            # -->      @a.length + 4 --> 7  (array var)
>     *@a + 4           # -->   (*@a).length + 4 --> 7  (list)
>     (@a,@a) + 4       # -->          3 + 3 + 4 --> 10 (list)
> 
> Alternatively, we could say that using a list in numeric context is a
> syntax error.  This is fine w/ me as well, but pointedly *doesn't*
> match the array behavior...

I think having them doing different things is reasonable -- they _are_
different.  An array is something tangible and as such can have
operations on it.  A list is something ephemeral, some scalars that
happen to be together right now but don't have a collective identity.
There can be operations on each of the individual items in a list but
having an operation on them as a group seems odd to me.

More practically, the length of a list is never interesting: a list by
definition must be hardcoded into the program so its length is known at
compile time.  Indeed it should be known by whoever typed it in!

> and would mean the second to last line would also be a syntax error.

That seems fine by me.  I can't see any reason why somebody would type
that rather than the version on the line above.

> I think the consistency of behavior probably means (3) wins.

Why should different things behave in the same way?

More pragmatically, somebody who has code with an explicitly hardcoded
list which is used in scalar context has probably made a mistake --
there are so many better ways (to take the above example) of hardcoding
the constant 7 into a program!  Why would somebody write out each
element of a list just to throw away most of the data and reduce it to
an integer?

In the case where somebody has made an error -- either a typo, or a
conceptual error by somebody still learning the language -- it's much
more useful to have a compilation error than for the code run but not
yield the desired behaviour.

Can somebody come up with a realistic example of where having a list be
interpreted as its length is genuinely useful and isn't more easily
written using some other syntax?

Smylers

Reply via email to