Skip to content

Instantly share code, notes, and snippets.

@raydiak
Created February 2, 2014 07:05
Show Gist options
  • Save raydiak/8764032 to your computer and use it in GitHub Desktop.
Save raydiak/8764032 to your computer and use it in GitHub Desktop.
fake 2D subscripts "is rw" problem
#!/usr/bin/env perl6
class Matrix4x4 is Array {
method at_pos ($i) is rw {
self.Array::[$_, $_+1, $_+2, $_+3] given $i * 4
}
method perl () {
self.defined ??
$?CLASS.perl ~ '.new(' ~ "\n" ~
(^4).map({
self[$_].map({.perl}).join(', ')
}).join(",\n").indent(4) ~
"\n)"
!!
'Matrix4x4'
}
}
my $m = Matrix4x4.new: |@(^16);
say 'Before:';
say $m.perl;
say "\nChanging value at [1][1] from $m[1][1] to 123";
$m[1][1] = 123;
say "\nAfter:";
say $m.perl; # shows no change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment