Skip to content

Commit

Permalink
Keep containers in Maps
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Jul 24, 2019
1 parent dcf8c16 commit 2ee82af
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions src/core/Map.pm6
Expand Up @@ -326,10 +326,12 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
nqp::ifnull(nqp::atkey($!storage,nqp::unbox_s(key.Str)),Nil)
}

multi method ASSIGN-KEY(Map:D: \key, Mu \value) {
die nqp::existskey($!storage,key.Str)
?? "Cannot change key '{key}' in an immutable {self.^name}"
!! "Cannot add key '{key}' to an immutable {self.^name}"
multi method ASSIGN-KEY(Map:D: \key, Mu \new) {
nqp::isnull(my \old := nqp::atkey($!storage,key.Str))
?? "Cannot add key '{key}' to an immutable {self.^name}"
!! nqp::iscont(old)
?? (old = new)
!! "Cannot change key '{key}' in an immutable {self.^name}"
}

# Directly copy from the other Map's internals: the only thing we need
Expand Down Expand Up @@ -393,7 +395,7 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
nqp::bindkey(
$!storage,
nqp::getattr(nqp::decont($x),Pair,'$!key').Str,
nqp::decont(nqp::getattr(nqp::decont($x),Pair,'$!value'))
nqp::getattr(nqp::decont($x),Pair,'$!value')
),
nqp::if(
(nqp::istype($x,Map) && nqp::not_i(nqp::iscont($x))),
Expand All @@ -408,7 +410,7 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
last => $x
).throw
),
nqp::bindkey($!storage,$x.Str,nqp::decont($y))
nqp::bindkey($!storage,$x.Str,$y)
)
)
)
Expand All @@ -417,24 +419,6 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
)
}

method !DECONTAINERIZE(--> Map:D) {
nqp::stmts(
(my \iter := nqp::iterator($!storage)),
nqp::while(
iter,
nqp::if(
nqp::iscont(nqp::iterval(nqp::shift(iter))),
nqp::bindkey(
$!storage,
nqp::iterkey_s(iter),
nqp::decont(nqp::iterval(iter)) # get rid of any containers
)
)
),
self
)
}

proto method STORE(Map:D: |) {*}
multi method STORE(Map:D: Map:D \map, :$INITIALIZE! --> Map:D) {
nqp::if(
Expand All @@ -446,7 +430,7 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
nqp::p6bindattrinvres(self,Map,'$!storage',other),
nqp::p6bindattrinvres(
self,Map,'$!storage',nqp::clone(other)
)!DECONTAINERIZE
)
),
self # nothing to do
),
Expand Down Expand Up @@ -478,7 +462,7 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
nqp::bindkey(
storage,
nqp::if(nqp::istype(key,Str),key,key.Str),
nqp::decont(itervalues.pull-one)
itervalues.pull-one
)
);
self
Expand Down

0 comments on commit 2ee82af

Please sign in to comment.