Skip to content

Commit

Permalink
Implement proper autovivifying array access
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Aug 7, 2010
1 parent ed4b778 commit 45b736d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions SAFE.setting
Expand Up @@ -348,9 +348,10 @@ sub infix:<~~>($t,$m) { ($m.defined) ?? ($m.ACCEPTS($t)) !! ($t.^does($m)) }
# TODO: Implement 'augment'
PRE-INIT {
Mu.HOW.add-method("defined", anon method defined() {
# rawcall to avoid putting a rw binding on self... TODO
Mu.HOW.add-method("defined", anon method defined is rawcall {
Q:CgOp { (box Bool (!= (null Dictionary<string,object>)
(getfield slots (cast DynObject (@ (l self)))))) }
(getfield slots (cast DynObject (@ (pos 0)))))) }
});
Mu.HOW.add-method("Bool", anon method Bool() { self.defined });
Mu.HOW.add-method("Str", anon method Str() {
Expand Down
17 changes: 17 additions & 0 deletions test2.pl
Expand Up @@ -58,4 +58,21 @@ sub postcircumfix:<[ ]> is rawcall {
my @arr = <a b c>;
is @arr.join("|"), 'a|b|c', "word splitter works";

my @narr;
@narr[0];
ok +@narr == 0, "rvalue reference to out of range value does not add";
@narr[2] = 5;
ok +@narr == 3, "assigning to element 2 makes length 3";
ok !(@narr[0].defined), "first element undefined";
ok !(@narr[1].defined), "second element undefined";
ok @narr[2] == 5, "third element properly assigned";

my @darr;
@darr[1][1];
ok +@darr == 0, "rvalue nested reference, no effect";
@darr[2][2] = 'pie';
ok +@darr == 3, "outer level vivifies elements";
ok @darr[2] ~~ Array, "inner Array created";
is @darr[2][2], 'pie', "inner value retained";

done-testing;

0 comments on commit 45b736d

Please sign in to comment.