Skip to content

Instantly share code, notes, and snippets.

@skids
Created August 17, 2015 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skids/ccb89f60bb57b95e0e01 to your computer and use it in GitHub Desktop.
Save skids/ccb89f60bb57b95e0e01 to your computer and use it in GitHub Desktop.
Deplorable hack for double iterator trace.
Be careful not to accidentally commit this.
$ perl6 -e 'my $a = (1,2,3).values; sub a { $a.iterator }; a(); $a.iterator;'
This Seq has already been iterated, and its values consumed
in sub a at -e:1
in block <unit> at -e:1
Actually thrown at:
in any at src/gen/m-Metamodel.nqp:2867
in block <unit> at -e:1
diff --git a/src/core/Seq.pm b/src/core/Seq.pm
index 8e2b45d..3edcb29 100644
--- a/src/core/Seq.pm
+++ b/src/core/Seq.pm
@@ -28,7 +28,7 @@ nqp::p6configposbindfailover(Positional, PositionalBindFailover);
my class Seq does Iterable does PositionalBindFailover {
# The underlying iterator that iterating this sequence will work its
# way through. Can only be obtained once.
- has Iterator $!iter;
+ has $!iter;
# The only valid way to create a Seq directly is by giving it the
# iterator it will consume and maybe memoize.
@@ -39,9 +39,18 @@ my class Seq does Iterable does PositionalBindFailover {
}
method iterator(Seq:D:) {
+ if nqp::istype($!iter, Failure) {
+ $!iter.throw;
+ }
my \iter = $!iter;
- X::Seq::Consumed.new.throw unless iter.DEFINITE;
- $!iter := Iterator;
+ if not iter.DEFINITE {
+ X::AdHoc.new(payload => "This Seq never had an Iterator").throw;
+ }
+ else {
+ my $fail = Failure.new(X::Seq::Consumed.new);
+ $fail.defined;
+ $!iter := $fail;
+ }
iter
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment