Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add proper type checks on anyof/allof
  • Loading branch information
lizmat committed Dec 21, 2014
1 parent 70cf4f5 commit b902e0d
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/core/Promise.pm
Expand Up @@ -4,7 +4,7 @@
my enum PromiseStatus (:Planned(0), :Kept(1), :Broken(2));
my class X::Promise::Combinator is Exception {
has $.combinator;
method message() { "Can only use $!combinator to combine other Promise objects" }
method message() { "Can only use $!combinator to combine defined Promise objects" }
}
my class X::Promise::CauseOnlyValidOnBroken is Exception {
has $.promise;
Expand Down Expand Up @@ -171,19 +171,15 @@ my class Promise {
$p
}

method anyof(Promise:U: *@promises) {
X::Promise::Combinator.new(combinator => 'anyof').throw
unless @promises >>~~>> Promise;
self!until_n_kept(@promises, 1)
}

method allof(Promise:U: *@promises) {
X::Promise::Combinator.new(combinator => 'allof').throw
unless @promises >>~~>> Promise;
self!until_n_kept(@promises, @promises.elems)
}
method anyof(Promise:U: *@p) { self!until_n_kept(@p, 1) }
method allof(Promise:U: *@p) { self!until_n_kept(@p, +@p) }

method !until_n_kept(@promises, Int $N) is hidden_from_backtrace {
X::Promise::Combinator.new(
combinator => callframe(2).my<&?ROUTINE>.name # skip <!> dispatcher
).throw if NOT_ALL_DEFINED_TYPE(@promises,Promise);

method !until_n_kept(@promises, Int $n) {
my int $n = $N;
my int $c = $n;
my $lock := nqp::create(Lock);
my $p = Promise.new;
Expand Down

0 comments on commit b902e0d

Please sign in to comment.