=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. 
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.  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];

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

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

=head1 IMPLEMENTATION

Very simple:  Just a few changes in the grammar.  Array references were 
always references to objects in one sense, it's now just defined that way 
on the surface.

Reply via email to