On Tue, 24 Sep 2002, Luke Palmer wrote: : =head1 TITLE : : Square brackets are the only list constructor : : =head1 VERSION : : Maintainer: Luke Palmer <[EMAIL PROTECTED]> : Date: 24 Sep 2002 : Number: 362 (?) : Version: 1 : : =head1 ABSTRACT : : This RFC responds to the fury on perl6-language about the redundancy of : parentheses and brackets with respect to list construction. It proposes : that only brackets may be used to create lists (or arrays), and : parentheses be left for grouping. : : =head1 DESCRIPTION : : Because of the addition of the flattening operator, parentheses in Perl 6, : when used as list constructors, are entirely redundant with brackets.
I cringe every time someone says "Parens construct lists in Perl 6." Parens don't construct lists in Perl 6. They merely group. The only difference from Perl 5 is that if they happen to group a comma in scalar context, the comma acts differently, not the parens. But the comma acts differently whether or not there are parens. It depends only on the context. I admit that it's difficult to get a comma into scalar context without parens, at least with the current precedence table. But if we swapped the precedence of comma and assignment, it would still be the case that $a = 1,2,3; generates a list because of the comma. The grouping would be implicit. (But there are other problems with doing that.) You can actually do the grouping without parens: $foo = do { 1,2,3 }; Gee, maybe we should get rid of parens, since they're redundant. :-) So the parens have little to do with the list construction. It's all the comma's fault. And in general, parentheses should not be used to construct lists in scalar context. It's better to use [] explicitly. In list context, the [] is not at all the same as (), because () is essentially a no-op in list context, while [] creates an array ref. I suppose we could make comma merely puke in scalar context rather than DWIM, at least optionally. : Additionally, parentheses have one inconsistency which brackets do not: : This is the following case, already shown on perl6-language: : : $a = (); # $a is a list reference with 0 elements : $a = (10); # $a is the scalar 10 : $a = (10, 20); # $a is a list reference with 2 elements : # ... : : If the ability to construct lists with parentheses is removed, so is this : inconsistency. I don't think that's an important inconsistency. : This has the added benefit that there is a significant : visual clue about when a list is being tossed around. This doesn't break : any convenience, just changes the look of it: : : # Perl 6 # Perl 5 : [$a, $b] ^= [$b, $a]; # ($a, $b) = ($b, $a) : print *[$a=$b],"\n"; # print(($a=$b), "\n"); : push @a: *[1,2,3]; # push @a, (1,2,3); : push @a: [1,2,3]; # push @a, [1,2,3]; I'd rather they just work the way people expect from Perl 5. Requiring people to say *[1,2,3] when they could say 1,2,3 is needless obfuscation. : =head2 Subroutines : : One place of quarrel is subroutine calls. Would this change the argument : list delimiters to brackets? I argue no, because they were never a : parenthesis-delimited list in the first place: : : print("Hello, World", "\n"); : print "Hello, World", "\n"; : : Thus, in this case, parentheses are used for grouping around the argument : list, not constructing it. Indeed. :-) : The following transformation has been : mentioned: : : print($a, $b); : : is equivilent to : : (print $a, $b); : : so no change is necessary on account of subroutine calls. : : =head2 Semantics : : The semantics of brackets need to be changed slightly to support this. : Specifically, instead of creating a list I<reference>, they create a list : I<object>. This object may be stored in an @array interface, or a $scalar : interface. The $scalar interface works the same way as array references : did. But the way @arrays are assigned changes in this manner: : : @array = [1, 2, 3]; # @array has 3 elements, not 1 : @array = [[1, 2, 3]]; # @array has 1 element: an array object : @array[0] = [1, 2, 3]; # same thing : : Slices work as they always have. I think if you're going to replace the whole array using a reference, it's better to use the explicit binding: @array := [1,2,3] It's awfully handy that the right side of a normal list assignment automatically flattens. And it's what Perl 5 programmers expect. They certainly won't expect [] to flatten. : =head2 Hashes : : For consistency, parentheses must be disallowed for hash construction as : well. The same semantic changes apply to braces. This fixes the common : mistake: : : %hash = { a => 1, b => 2 }; : : will now DWIM. Well, that one can be caught, if not the other one. Again, the right approach to dealing with references is probably to bind: %hash := { a => 1, b => 2 }; Larry