Perl6 RFC Librarian wrote:

> Which again, can be used in the appropriate contexts. Note this allows
> you to maintain arrayref objects automatically as well:
>
>    package Johnson;
>    sub new {
>        my $class = shift;
>        my $self = [];
>        $self->[0]->[2]->[3] :raccess('size') = undef;
>        $self->[4]->[1] :laccess('name') = undef;
>        return bless $self, $class;
>    }
>
> Although an arrayref is usually not the best data representation for
> direct-access objects.

This is an interesting comment to be made about an interesting side effect of
this proposal.

One of the big complaints about today's perl objects is their slowness at
accessing member data, which is a direct result of hashes being used as the
base data type for the underlying member data items.

Granted, hashes allow additional keys to be added (although collision
problems can exist) to the _same_ data structure with conceptual ease.
However, you can always make an array bigger too.

I suspect that the primary reason to use hashes for object data members is
the ease of named access to the individual members.  Remembering the position
of the data item by number in an array would be much harder on the
programmer.

Now you use a complex data structure in your above example, but let's look at
a simple one:

package Smith;
sub new {
  my $class = shift;
  my $self = [];
  $self ->[0] = 10;
  $self ->[1] = 20;
  $self ->[2] = 30;
  return bless $self, $class;
}

$x = new Smith;
print $x->[0].'/'.$x->[1].'/'.$x->[2];

Now augment this with accessors (maybe some should be private, some public?),
and we get:

package Smith;
sub new {
  my $class = shift;
  my $self = [];
  $self ->[0] :raccess('month')= 10;
  $self ->[1] :raccess('day')= 20;
  $self ->[2] :raccess('year')= 30;
  return bless $self, $class;
}

$x = new Smith;
print $x->month.'/'.$x->day.'/'.$x->year;

Suddenly...

(1) array elements can be accessed by name
(2) member data can be looked up quicker (by array index, rather than by
hashed name)
[Granted, this means the compiler looks it up once per reference, but the
runtime uses it potentially many times per reference]

Some additional syntactic sugar might eliminate the need to even code the
explicit array indexes in new... especially if we make this a general feature
of arrays, rather than being OO specific... how about...

new RFC:

The syntax

    $foo['element']

would be interpreted as a reference to @foo.  If the namespace for @foo
contains 'element', that member of @foo is the interpretation.  If the
namespace for @foo does not contain 'element', then 'element' is added to the
namespace for @foo, the size of @foo is increased by 1, and the member
'element' refers to the newly added item in @foo.

So, starting with

   my @foo;  # empty array
   $foo ['month'] = 10;  #  $#foo == 1, $foo[0] == 10
   $foo ['day'] = 20;   # $#foo == 2, $foo [1] == 20
   $foo ['year'] = 30;   # $#foo = 3, $foo [2] == 30

We achieve the same effect.  There is a clear parallel between this and

   my %foo;
   $foo{'month'} = 10;
   $foo{'day'} = 20;
   $foo{'year'} = 30;

However, the lookups for @foo are done at compile time, the lookups for %foo
are done at runtime.

Getting back to RFC 163 (v2), for arrays which have namespaces, the :raccess
and :laccess would use those default names, if not overridden by a user
specified one.
--
Glenn
=====
Even if you're on the right track,
you'll get run over if you just sit there.
                       -- Will Rogers



____________NetZero Free Internet Access and Email_________
Download Now     http://www.netzero.net/download/index.html
Request a CDROM  1-800-333-3633
___________________________________________________________

Reply via email to