Skip to content

Commit

Permalink
Make 'my %h is Set(|Hash) = ...;' DWIM
Browse files Browse the repository at this point in the history
- in response to RT #132352 and RT #132353
- make also sure we can initialize Sets only once
  • Loading branch information
lizmat committed Oct 29, 2017
1 parent af3624d commit 1949a2b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/core/Set.pm
Expand Up @@ -11,6 +11,21 @@ my class Set does Setty {
set()
)
}
method STORE(*@pairs --> Set:D) {
nqp::if(
(my $iterator := @pairs.iterator).is-lazy,
Failure.new(X::Cannot::Lazy.new(:action<initialize>,:what(self.^name))),
nqp::if(
$!elems || nqp::eqaddr(self,set()),
X::Immutable.new( method => 'STORE', typename => self.^name ).throw,
self.SET-SELF(
Rakudo::QuantHash.ADD-PAIRS-TO-SET(
nqp::create(Rakudo::Internals::IterationSet), $iterator
)
)
)
)
}
multi method new(Set:_:) {
nqp::if(
nqp::eqaddr(self.WHAT,Set),
Expand Down
15 changes: 15 additions & 0 deletions src/core/SetHash.pm
Expand Up @@ -10,6 +10,21 @@ my class SetHash does Setty {
)
}

method STORE(*@pairs --> SetHash:D) {
nqp::if(
(my $iterator := @pairs.iterator).is-lazy,
Failure.new(X::Cannot::Lazy.new(:action<initialize>,:what(self.^name))),
nqp::stmts(
nqp::bindattr(self,::?CLASS,'$!elems',
Rakudo::QuantHash.ADD-PAIRS-TO-SET(
nqp::create(Rakudo::Internals::IterationSet), $iterator
)
),
self
)
)
}

#--- selector methods

multi method grab(SetHash:D:) {
Expand Down

0 comments on commit 1949a2b

Please sign in to comment.