On Saturday, February 15, 2003, at 08:47 AM, David Storrs wrote:
IMHO the only reasonable possibilities are (2) or (3)... the others are much rarer in practice, and too prone to accidental-invocation-with-baffling-results.I can see five possible courses here:1) We decide that my suggestion is a bad one and do nothing with it. That's fine; I am not wedded to it, I just thought it was an interesting idea that I wanted to raise. 2) (4, 1, 2) + 7 returns (9). This is C comma behavior, and I always found it incredibly non-intuitive. I'd really like to get away from this, even if it means that this expression is a fatal error "Can't add scalar to list". 3) (4, 1, 2) + 7 returns (10), by adding 7 to the length of the list. This makes lists look even more like arrays, and doesn't really add any new power to the language. 4) (4, 1, 2) + 7 returns (11, 8, 9). This is a convenient shorthand for the vector syntax, IMO. 5) (4, 1, 2) + 7 returns (14). That is, the list is collapsed into a datatype matching the "RHS" by iteratively applying the operator in question to the list elements, and then the item on the RHS of the operator is applied. I'm not sure this is useful; I'm just exploring options.
Agreed, however, that (2) is icky. My worry has been that removing C-comma behavior would break common constructs, but I haven't been able to find any that would really break (except obfuscated ones that would be better written in some other fashion anyway.) Statements like:
foo() or (warn("blah"), next);
work either way, because they don't rely on getting the "scalar value" of the list.
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... and would mean the second to last line would also be a syntax error.
I think the consistency of behavior probably means (3) wins.
MikeL