Skip to content

Commit b8e8aa2

Browse files
committed
Explains how to use IterationEnd via binding.
Plus some reflow here and there. Closes #2291
1 parent 5cb6bd7 commit b8e8aa2

File tree

2 files changed

+52
-21
lines changed

2 files changed

+52
-21
lines changed

doc/Language/control.pod6

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -778,11 +778,12 @@ All these forms may produce a return value the same way C<loop> does.
778778
=head1 X<return|control flow, return>
779779
780780
The sub C<return> will stop execution of a subroutine or method, run all
781-
relevant L<phasers|/language/phasers#Block_phasers> and provide the given
782-
return value to the caller. The default return value is C<Nil>. If a return
783-
L<type constraint|/type/Signature#Constraining_return_types> is provided it
784-
will be checked unless the return value is C<Nil>. If the type check fails the
785-
exception L<X::TypeCheck::Return|/type/X::TypeCheck::Return> is thrown. If it
781+
relevant L<phasers|/language/phasers#Block_phasers> and provide the
782+
given return value to the caller. The default return value is C<Nil>. If
783+
a return L<type constraint|/type/Signature#Constraining_return_types> is
784+
provided it will be checked unless the return value is C<Nil>. If the
785+
type check fails the exception
786+
L<X::TypeCheck::Return|/type/X::TypeCheck::Return> is thrown. If it
786787
passes a control exception is raised and can be caught with
787788
L<CONTROL|/language/phasers#CONTROL>.
788789
@@ -791,15 +792,15 @@ lexical scope of that block, no matter how deeply nested. Please note
791792
that a C<return> in the root of a package will fail at runtime. A
792793
C<return> in a block that is evaluated lazily (e.g. inside C<map>) may
793794
find the outer lexical routine gone by the time the block is executed.
794-
In almost any case C<last> is the better alternative. Please check L<the
795-
functions documentation|/language/functions#Return_values> for more
796-
information on how return values are handled and produced.
795+
In almost any case C<last> is the better alternative. Please check
796+
L<the functions documentation|/language/functions#Return_values> for
797+
more information on how return values are handled and produced.
797798
798799
799800
=head1 X<return-rw|control flow, return-rw>
800801
801-
The sub C<return> will return values, not containers. Those are immutable
802-
and will lead to runtime errors when attempted to be mutated.
802+
The sub C<return> will return values, not containers. Those are
803+
immutable and will lead to runtime errors when attempted to be mutated.
803804
804805
sub s(){ my $a = 41; return $a };
805806
say ++s();

doc/Type/Iterator.pod6

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ sequence. Users usually don't have to care about iterators, their usage
1313
is hidden behind iteration APIs such as C<for @list { }>, L<map>,
1414
L<grep>, L<head>, L<tail>, L<skip> and list indexing with C<.[$idx]>.
1515
16-
The main API is the C<pull-one> method, which either returns the next value,
17-
or the sentinel value C<IterationEnd> if no more elements are available. Each
18-
class implementing C<Iterator> B<must> provide a C<pull-one> method. All other
19-
non-optional Iterator API methods are implemented in terms of C<pull-one>, but
20-
can also be overridden by consuming classes for performance or other reasons.
21-
There are also optional Iterator API methods that will only be called if they
22-
are implemented by the consuming class: these are B<not> implemented by the
16+
The main API is the C<pull-one> method, which either returns the next
17+
value, or the sentinel value C<IterationEnd> if no more elements are
18+
available. Each class implementing C<Iterator> B<must> provide a
19+
C<pull-one> method. All other non-optional Iterator API methods are
20+
implemented in terms of C<pull-one>, but can also be overridden by
21+
consuming classes for performance or other reasons. There are also
22+
optional Iterator API methods that will only be called if they are
23+
implemented by the consuming class: these are B<not> implemented by the
2324
Iterator role.
2425
25-
=head1 IterationEnd
2626
X<|IterationEnd>
27+
=head1 IterationEnd
2728
2829
Iterators only allow one iteration over the entire sequence. It's
2930
forbidden to make attempts to fetch more data, once C<IterationEnd> has
@@ -58,9 +59,38 @@ for @a -> $a, $b {
5859
# OUTPUT: «[1 3]␤[5 8]␤»
5960
=end code
6061
61-
The only valid use of the sentinel value C<IterationEnd> in a program
62-
is identity comparison (using C<=:=>) with the result of a method in the
63-
iterator API. Any other behavior is undefined and implementation dependent.
62+
The only valid use of the sentinel value C<IterationEnd> in a program is
63+
identity comparison (using C<=:=>) with the result of a method in the
64+
iterator API. Any other behavior is undefined and implementation
65+
dependent.
66+
67+
Please bear in mind that C<IterationEnd> is a constant, so if you are
68+
going to compare it against the value of a variable, this variable will
69+
have to be bound, not assigned. Comparing directly to the output of
70+
C<pull-one> will work.
71+
72+
=begin code
73+
my $it = (1,2).iterator;
74+
$it.pull-one for ^2;
75+
say $it.pull-one =:= IterationEnd; # OUTPUT: «True␤»
76+
=end code
77+
78+
However, if we use a variable we and we assign it, the result will be
79+
incorrect:
80+
81+
=begin code
82+
my $it = (1,2).iterator;
83+
$it.pull-one for ^2;
84+
my $is-it-the-end = $it.pull-one;
85+
say $is-it-the-end =:= IterationEnd; # OUTPUT: «False␤»
86+
=end code
87+
88+
So we'll have to bind the variable to make it work:
89+
90+
=begin code :preamble<my $it>
91+
my $is-it-the-end := $it.pull-one;
92+
say $is-it-the-end =:= IterationEnd; # OUTPUT: «True␤»
93+
=end code
6494
6595
=head1 Methods
6696

0 commit comments

Comments
 (0)