David L. Nicol writes:
> Looks like if we give the data type control over what
> the meaning of square brackets after it is, the rest
> becomes example code.  I think this s covered in the
> horribly misnamed http://dev.perl.org/rfc/115.pod which
> covers overloading bracketing.

The big problem with 115 is that it doesn't mention how to
disambiguate

  $foo[]

where $foo is an array, from

  $foo[]

where @foo is an array.

The
  $foo()
syntax doesn't suffer from this ambiguity, I believe.  I'm not hot on
it because it's new.  The curmudgeon in me believes that it should be
possible to do what you want to do with the current array subscript
syntax. 


Better, I think, would be a merging of tie() and overload() so
that you can make objects that act like Perl arrays.  But
$foo[EXPR] presently puts EXPR into scalar context.  But slices
with : would require the FETCH method to be called with all the
info in the slice.

(idea!) Unless : was shorthand for [] building.

  X:Y:Z

is equivalent to

  [X, Y, Z]

Then your FETCH method would still be called with one argument, and []
processing for objectitied arrays wouldn't require specialized
processing of the [].


Some thoughts on what this would look like in a program:

  my PDL @foo;  # ties @foo to PDL?  Larry might have suggestions
                # for a syntax
  $foo[2] = 5;  # $object_behind_foo -> STORE(2, 5)

Then you'd get a slice by saying:

  @foo[2:5] = (9..12);
                # $object_behind_foo -> STORE( [2,5], 9,10,11,12 );

  $foo[2:5] = (9..12);
                # same thing.  If you didn't want it to be the
                # same thing, make the X:Y in list context actually
                # produce the list of values from X to Y

  my PDL @bar = @foo[2:5];
                # ties @bar, calls
                # $obj_bar->STORE([], $obj_foo->FETCH([2,5]))

  $foo[2][5:9]
                # $obj_foo->FETCH(2, [5..9])

So you know you have a multidim access because each dimension is
a different argument to FETCH.  Normal (one-dim) array accesess
result in single arguments to FETCH.

This doesn't let you have a simple list of indices in a single FETCH.
It doesn't distinguish between:

  @foo[2,5]
  @foo[2:5]

If you say the former is FETCH(2,5) then you lose the multidim.
nature where $foo[2][5] becomes FETCH(2,5).  Ugh.  I'm trying to do
this by adding as little new stuff as possible (e.g., lazy iterators,
$foo(...) syntax, etc.).


I think that at this point someone (I'm swamped right now) needs to go
and read up on how Python does the tie overloading merge.  Even a
pointer to online documentation would be good.  If they've already
solved these problems, we should see what they did.

Nat

Reply via email to