Skip to content
Permalink
Browse files

Allow assignment of Nil to type constrained hashes

Also simplify the code, making it a few percent faster as well.
  • Loading branch information...
lizmat committed Oct 8, 2018
1 parent a6b43a5 commit 605bcb391f75ab6e6a8f2b340fd664c4a6013484
Showing with 12 additions and 30 deletions.
  1. +12 −30 src/core/Hash.pm6
@@ -429,37 +429,19 @@ my class Hash { # declared in BOOTSTRAP
)
}

# These ASSIGN-KEY candidates are only needed because of:
# my Int %h; try %h<a> = "foo"; dd %h
# leaving an uninitialized Int for key <a> in the hash. If
# we could live with that, then these candidates can be
# removed. However, there are spectest covering this
# eventuality, so to appease roast, we need these.
multi method ASSIGN-KEY(::?CLASS:D: Str:D \key, Mu \assignval) is raw {
my \storage := nqp::getattr(self, Map, '$!storage');
nqp::if(
nqp::existskey(storage, nqp::unbox_s(key)),
(nqp::atkey(storage, nqp::unbox_s(key)) = assignval),
nqp::bindkey(
storage,
nqp::unbox_s(key),
nqp::p6scalarfromdesc(
nqp::getattr(self,Hash,'$!descriptor')) = assignval
)
)
}
multi method ASSIGN-KEY(::?CLASS:D: \key, Mu \assignval) is raw {
my \storage := nqp::getattr(self, Map, '$!storage');
my str $key = nqp::unbox_s(key.Str);
method ASSIGN-KEY(::?CLASS:D: Mu \key, Mu \assignval) is raw {
my \storage := nqp::getattr(self, Map, '$!storage');
my \which := key.Str;
my \existing := nqp::atkey(storage,which);
nqp::if(
nqp::existskey(storage,$key),
(nqp::atkey(storage,$key) = assignval),
nqp::bindkey(
storage,
nqp::unbox_s(key.Str),
nqp::p6scalarfromdesc(
nqp::getattr(self,Hash,'$!descriptor')) = assignval
)
nqp::isnull(existing),
nqp::stmts(
((my \scalar := nqp::p6scalarfromdesc( # assign before
nqp::getattr(self,Hash,'$!descriptor') # binding to get
)) = assignval), # type check
nqp::bindkey(storage,which,scalar)
),
(existing = assignval)
)
}
method BIND-KEY(\key, TValue \value) is raw {

0 comments on commit 605bcb3

Please sign in to comment.
You can’t perform that action at this time.