Skip to content

Commit

Permalink
Make %h{;} slices with * non-assignable
Browse files Browse the repository at this point in the history
Because a slice with * at any level, is non-deterministic.  Making it
die with an appropriate error message like:

    $ raku -e 'my %h; %h{*} = 42'
    Cannot assign to *, as the order of keys is non-deterministic

would require mixing in some logic in the return List, or replacing
any containers with custom containers that produce that error message.
Which would incur a significant overhead for the case when no assignment
is attempted.
  • Loading branch information
lizmat committed Oct 16, 2020
1 parent 564faa6 commit 93503c2
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/core.e/hash_multislice.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ multi sub postcircumfix:<{; }>(\initial-SELF, @indices,
SELF.AT-KEY($_),
nqp::atpos($indices,$next-dim),
$next-dim
) for SELF.keys; # NOTE: not reproducible!
) for SELF.keys;
}
else {
EXISTS-KEY-recursively(
Expand Down Expand Up @@ -78,7 +78,7 @@ multi sub postcircumfix:<{; }>(\initial-SELF, @indices,
SELF.AT-KEY($_),
nqp::atpos($indices,$next-dim),
$next-dim
) for SELF.keys; # NOTE: not reproducible!
) for SELF.keys;
}
else {
DELETE-KEY-recursively(
Expand Down Expand Up @@ -267,6 +267,7 @@ multi sub postcircumfix:<{; }>(\initial-SELF, @indices,

# no adverbs whatsoever
else {
my int $non-deterministic;
sub AT-KEY-recursively(\SELF, \idx, int $dim --> Nil) {
my int $next-dim = $dim + 1;
if nqp::istype(idx, Iterable) && nqp::not_i(nqp::iscont(idx)) {
Expand All @@ -275,10 +276,10 @@ multi sub postcircumfix:<{; }>(\initial-SELF, @indices,
}
elsif $next-dim < $dims {
if nqp::istype(idx,Whatever) {
$return-list = 1;
$return-list = $non-deterministic = 1;
AT-KEY-recursively(
SELF.AT-KEY($_), nqp::atpos($indices,$next-dim), $next-dim
) for SELF.keys; # NOTE: not reproducible
) for SELF.keys;
}
else {
AT-KEY-recursively(
Expand All @@ -288,7 +289,7 @@ multi sub postcircumfix:<{; }>(\initial-SELF, @indices,
}
# $next-dim == $dims, reached leaves
elsif nqp::istype(idx,Whatever) {
$return-list = 1;
$return-list = $non-deterministic = 1;
nqp::push(target,SELF.AT-KEY($_)) for SELF.keys;
}
else {
Expand All @@ -297,6 +298,16 @@ multi sub postcircumfix:<{; }>(\initial-SELF, @indices,
}

AT-KEY-recursively(initial-SELF, nqp::atpos($indices,0), 0);

# decont all elements if non-deterministic to disallow assignment
if $non-deterministic {
my int $i = -1;
my int $elems = nqp::elems(target);
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::bindpos(target,$i,nqp::decont(nqp::atpos(target,$i)))
);
}
}

$return-list
Expand Down

0 comments on commit 93503c2

Please sign in to comment.