Skip to content

Commit

Permalink
Handle QuantHash:U coercion differently
Browse files Browse the repository at this point in the history
This should fix some issues with set operators (RT ##131300).

Before this, Set.Set would return Set.  Now it returns set(Set).  So
instead of not coercing a QuantHash type object, we now coerce it into
a defined QuantHash object with the type object as its member.

This is consistent with e.g. Int.Set, which already coerced to set(Int).
  • Loading branch information
lizmat committed May 13, 2017
1 parent ba0581d commit 407bce1
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 35 deletions.
24 changes: 18 additions & 6 deletions src/core/Any.pm
Expand Up @@ -432,12 +432,24 @@ my class Any { # declared in BOOTSTRAP
}
method FLATTENABLE_HASH() is nodal { nqp::hash() }

method Set() is nodal { Set.new-from-pairs(self.list) }
method SetHash() is nodal { SetHash.new-from-pairs(self.list) }
method Bag() is nodal { Bag.new-from-pairs(self.list) }
method BagHash() is nodal { BagHash.new-from-pairs(self.list) }
method Mix() is nodal { Mix.new-from-pairs(self.list) }
method MixHash() is nodal { MixHash.new-from-pairs(self.list) }
proto method Set(|) is nodal { * }
multi method Set(Any:) { Set.new-from-pairs(self.list) }

proto method SetHash(|) is nodal { * }
multi method SetHash(Any:) { SetHash.new-from-pairs(self.list) }

proto method Bag(|) is nodal { * }
multi method Bag(Any:) { Bag.new-from-pairs(self.list) }

proto method BagHash(|) is nodal { * }
multi method BagHash(Any:) { BagHash.new-from-pairs(self.list) }

proto method Mix(|) is nodal { * }
multi method Mix(Any:) { Mix.new-from-pairs(self.list) }

proto method MixHash(|) is nodal { * }
multi method MixHash() { MixHash.new-from-pairs(self.list) }

# XXX GLR does this really need to force a list?
method Supply() is nodal { self.list.Supply }

Expand Down
4 changes: 2 additions & 2 deletions src/core/Bag.pm
Expand Up @@ -42,8 +42,8 @@ my class Bag does Baggy {
}

#--- coercion methods
method Bag() is nodal { self }
method Mix() is nodal {
multi method Bag(Bag:D:) { self }
multi method Mix(Bag:D:) {
nqp::p6bindattrinvres(nqp::create(Mix),Mix,'%!elems',%!elems)
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/BagHash.pm
Expand Up @@ -59,7 +59,7 @@ my class BagHash does Baggy {
#--- introspection methods
method total() { Rakudo::QuantHash.BAG-TOTAL(self.raw_hash) }

method Bag(:$view) is nodal {
multi method Bag(BagHash:D: :$view) {
nqp::if(
(my $raw := self.raw_hash) && nqp::elems($raw),
nqp::p6bindattrinvres(
Expand All @@ -69,8 +69,8 @@ my class BagHash does Baggy {
bag()
)
}
method BagHash() is nodal { self }
method Mix() is nodal {
multi method BagHash(BagHash:D:) { self }
multi method Mix(BagHash:D:) {
nqp::if(
(my $raw := self.raw_hash) && nqp::elems($raw),
nqp::p6bindattrinvres(nqp::create(Mix),Mix,'%!elems',%!elems.clone),
Expand Down
12 changes: 6 additions & 6 deletions src/core/Baggy.pm
Expand Up @@ -637,8 +637,8 @@ my role Baggy does QuantHash {
)
)
}
method Set() is nodal { self!SETIFY(Set) }
method SetHash() is nodal { self!SETIFY(SetHash) }
multi method Set(Baggy:D:) { self!SETIFY(Set) }
multi method SetHash(Baggy:D:) { self!SETIFY(SetHash) }

method !BAGGIFY(\type) {
nqp::if(
Expand Down Expand Up @@ -684,8 +684,8 @@ my role Baggy does QuantHash {
)
}

method Bag() is nodal { self!BAGGIFY(Bag) }
method BagHash() is nodal { self!BAGGIFY(BagHash) }
multi method Bag(Baggy:D:) { self!BAGGIFY(Bag) }
multi method BagHash(Baggy:D:) { self!BAGGIFY(BagHash) }

method !MIXIFY(\type) {
nqp::if(
Expand Down Expand Up @@ -716,8 +716,8 @@ my role Baggy does QuantHash {
)
}

method Mix() is nodal { self!MIXIFY(Mix) }
method MixHash() is nodal { self!MIXIFY(MixHash) }
multi method Mix(Baggy:D:) { self!MIXIFY(Mix) }
multi method MixHash(Baggy:D:) { self!MIXIFY(MixHash) }

method clone() {
nqp::if(
Expand Down
4 changes: 2 additions & 2 deletions src/core/Hash.pm
Expand Up @@ -777,8 +777,8 @@ my class Hash { # declared in BOOTSTRAP
)
)
}
method Set() is nodal { self!SETIFY(Set ) }
method SetHash() is nodal { self!SETIFY(SetHash) }
multi method Set(::?CLASS:D:) { self!SETIFY(Set ) }
multi method SetHash(::?CLASS:D:) { self!SETIFY(SetHash) }
}
method ^parameterize(Mu:U \hash, Mu:U \t, |c) {
if c.elems == 0 {
Expand Down
4 changes: 2 additions & 2 deletions src/core/Iterable.pm
Expand Up @@ -157,8 +157,8 @@ my role Iterable {
)
)
}
method Set() is nodal { self!SETIFY(Set) }
method SetHash() is nodal { self!SETIFY(SetHash) }
multi method Set(Iterable:D:) { self!SETIFY(Set) }
multi method SetHash(Iterable:D:) { self!SETIFY(SetHash) }
}

#?if jvm
Expand Down
4 changes: 2 additions & 2 deletions src/core/Map.pm
Expand Up @@ -433,8 +433,8 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
)
)
}
method Set() is nodal { self!SETIFY(Set) }
method SetHash() is nodal { self!SETIFY(SetHash) }
multi method Set(Map:D:) { self!SETIFY(Set) }
multi method SetHash(Map:D:) { self!SETIFY(SetHash) }
}

multi sub infix:<eqv>(Map:D \a, Map:D \b) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/Mix.pm
Expand Up @@ -45,7 +45,7 @@ my class Mix does Mixy {
}

#--- coercion methods
method Mix() is nodal { self }
multi method Mix(Mix:D:) { self }

method clone() { nqp::clone(self) }

Expand Down
4 changes: 2 additions & 2 deletions src/core/MixHash.pm
Expand Up @@ -59,7 +59,7 @@ my class MixHash does Mixy {
multi method new(MixHash:_:) { nqp::create(self) }

#--- coercion methods
method Mix(:$view) is nodal {
multi method Mix(MixHash:D: :$view) {
nqp::if(
(my $raw := self.raw_hash) && nqp::elems($raw),
nqp::p6bindattrinvres(
Expand All @@ -69,7 +69,7 @@ my class MixHash does Mixy {
mix()
)
}
method MixHash() is nodal { self }
multi method MixHash(MixHash:D:) { self }

#--- iterator methods
sub proxy(Mu \iter,Mu \storage) is raw {
Expand Down
4 changes: 3 additions & 1 deletion src/core/QuantHash.pm
Expand Up @@ -5,7 +5,9 @@ my role QuantHash does Associative {
method Real ( --> Real:D) { self.total.Real }

method Capture() { self.Hash.Capture }
method list() { self.pairs.cache }

multi method list(QuantHash:U:) { self.Any::list }
multi method list(QuantHash:D:) { self.pairs.cache }

method fmt(QuantHash: Cool $format = "%s\t\%s", $sep = "\n") {
nqp::iseq_i(nqp::sprintfdirectives( nqp::unbox_s($format.Stringy)),1)
Expand Down
4 changes: 2 additions & 2 deletions src/core/Set.pm
Expand Up @@ -87,8 +87,8 @@ my class Set does Setty {
X::Immutable.new( method => 'grabpairs', typename => self.^name ).throw;
}

method Set() is nodal { self }
method SetHash() is nodal {
multi method Set(Set:D:) { self }
multi method SetHash(Set:D:) {
nqp::if(
$!elems,
nqp::p6bindattrinvres(
Expand Down
4 changes: 2 additions & 2 deletions src/core/SetHash.pm
Expand Up @@ -117,7 +117,7 @@ my class SetHash does Setty {
)
}

method Set(SetHash:D: :$view) is nodal {
multi method Set(SetHash:D: :$view) {
nqp::if(
$!elems,
nqp::p6bindattrinvres(
Expand All @@ -127,7 +127,7 @@ my class SetHash does Setty {
nqp::create(Set)
)
}
method SetHash(SetHash:D:) is nodal { self }
multi method SetHash(SetHash:D:) { self }

multi method AT-KEY(SetHash:D: \k --> Bool:D) is raw {
Proxy.new(
Expand Down
8 changes: 4 additions & 4 deletions src/core/Setty.pm
Expand Up @@ -214,10 +214,10 @@ my role Setty does QuantHash {
nqp::create(type)
)
}
method Bag() { self!BAGGIFY(Bag) }
method BagHash() { self!BAGGIFY(BagHash) }
method Mix() { self!BAGGIFY(Mix) }
method MixHash() { self!BAGGIFY(MixHash) }
multi method Bag(Setty:D:) { self!BAGGIFY(Bag) }
multi method BagHash(Setty:D:) { self!BAGGIFY(BagHash) }
multi method Mix(Setty:D:) { self!BAGGIFY(Mix) }
multi method MixHash(Setty:D:) { self!BAGGIFY(MixHash) }

method raw_hash() is raw { $!elems }
method hll_hash() is raw {
Expand Down

0 comments on commit 407bce1

Please sign in to comment.