Matt Diephouse wrote:

Is it possible to assign to an array slice?

 @array[0..4] = ( 0..4 ); # splice @array, 0, 5, 0..4

If so (and I'm hoping it is), is there an equivalent of Ruby's `[]=`
method? (Is there a way to define this behavior within my own
array-like classes?)


I assign to array and hash slices constantly in Perl 5. I would be shocked if Perl 6 did not support doing the same. One of the things I point to non-Perl people about why I like Perl over other languages is a statement like (p5) C< @b{keys %a} = values %a; >, and then explain what it does, and how it's different from C< %b = %a >.

As for defining your own, that falls on the as yet unwritten S14 concerning the Perl 6 version of "tie". At a minimum, I think you'd need to say something like

 multi sub postcircumflex::<[ ]>(MyArray $obj : [EMAIL PROTECTED]) is rw {...}

but I'll wait for S14 before speculating further.

Can I use slice notation when dealing with strings?

  say $string[-1]; # say substr($string, -1);
  $string[0..2] = "Hello";

I know some people find strings-as-arrays too much like C, but I think
it can be convenient. (And I happen to think `splice` and `substr` are
really ugly.) I'm close to suggesting that we don't need `splice` and
`substr` at all, but there are times when it's more convenient to
specify with a starting point and a length than with a range.

At present, I'd say the answer is "no", but generating a tied Array of Chars or Ints to it will be very easy to do.

I don't think you can get rid of C<splice> and C<substr> that easily. Besides being really handy to have around, they also have the ability to delete and insert things of arbitrary length from the middle. And that's not something you can do with slices, unless you start stiching the prefix, replacement, and suffix slices on your own, which requires a lot of copying that could be skipped.

-- Rod Adams






Reply via email to