Skip to content

Commit 2b3c920

Browse files
committed
Bring page on Nil into sync with post-GLR reality.
1 parent f86c26d commit 2b3c920

File tree

1 file changed

+92
-24
lines changed

1 file changed

+92
-24
lines changed

doc/Type/Nil.pod

Lines changed: 92 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,66 @@
22
33
=TITLE class Nil
44
5-
=SUBTITLE Absence of a value
5+
=SUBTITLE Absence of a value or a benign failure
66
7-
=comment TODO: This page seems to documents a previous design iteration of Nil,
8-
not the current one. Needs re-checking and rewriting. I only
9-
updated the subtitle for now.
7+
class Nil is Cool { }
108
11-
class Nil is Iterator { }
9+
The value C<Nil> may be used to fill a spot where a value would normally
10+
go, and in so doing, explicitly indicate that no value is present. It
11+
may also used as a cheaper and less explosive alternative to a
12+
L<C<Failure>|/type/Failure>.
1213
13-
Represents an empty list in list context, and an undefined value in item
14-
context.
14+
The class C<Nil> is the same exact thing as its only possible value, C<Nil>.
1515
16-
C<Nil>, when assigned, evaporates into the type object of the type of the
17-
container you assigned it into.
16+
say Nil === Nil.new #-> True
17+
18+
Along with C<Failure>, C<Nil> is one of the two types which may
19+
always be returned from a routine even when the routine specifies a
20+
particular return type. It may also be returned regardess of the
21+
definedness of the return type, however, C<Nil> is considered undefined
22+
for all other purposes.
23+
24+
sub a ( --> Int:D ) { return Nil }
25+
a().say; #-> Nil
26+
27+
C<Nil> is what is returned from empty routines or clauses, or routines
28+
that use a bare C<return> statement.
29+
30+
sub a { }; a().say; #-> Nil
31+
sub b { return }; b().say; #-> Nil
32+
say (if 1 { }); #-> Nil
33+
{ ; }().say; #-> Nil
34+
say EVAL ""; #-> Nil
35+
36+
In a list, C<Nil> takes the space of one value. Iterating a C<Nil>
37+
behaves like iteration of any non-iterable value, producing a sequence
38+
of one C<Nil>.
39+
40+
(1,Nil,3).elems.say; #-> 3
41+
(for Nil { $_ }).perl.say; #-> (Nil,)
42+
43+
Any method call on C<Nil> other than those specifically listed below,
44+
and consequently, any subscripting operation, will succeed and return
45+
C<Nil>.
46+
47+
say Nil.ITotallyJustMadeThisUp; #-> Nil
48+
say (Nil)[100]; #-> Nil
49+
say (Nil){100}; #-> Nil
50+
51+
When assigned, a C<Nil> evaporates into the type object of the type
52+
of the container you assigned it into.
1853
1954
my Int $x = 42;
2055
$x = Nil;
21-
$x.say; #-> (Int)
56+
$x.say; #-> (Int)
2257
23-
It is for this reason that assigning an untyped container as C<Nil> will result
24-
in an L<(Any)> object.
58+
Because an untyped variable is type C<Any>, assigning a C<Nil> to one
59+
will result in an L<(Any)> type object.
2560
2661
my $x = Nil;
2762
$x.say; #-> (Any)
28-
my Int $y = $x; # error: Type check failed in assignment to '$y'; expected 'Int' but got 'Any'
29-
30-
This is because an untyped container is C<Any> until proven otherwise, and
31-
the C<Nil> evaporates.
63+
my Int $y = $x; # error: Type check failed in assignment to '$y';
64+
# expected 'Int' but got 'Any'
3265
3366
If you are looking for a variable which transforms objects into type objects
3467
when said variable appears on the RHS, you can type the container as C<Nil>.
@@ -45,14 +78,13 @@ assigning C<Nil> to a variable which has a default will restore that default.
4578
for $x,$y -> $val is rw { $val = $reset unless $val > 0 }
4679
$x.say; #-> 42
4780
48-
To make C<Nil> stick to a container use binding. To test against C<Nil> use
49-
C<===> and C<any>.
81+
=head1 Methods
5082
51-
my @c := 1,Nil,2;
52-
dd @c; # (1, Nil, 2)
53-
say so Nil === any(@c); # True
83+
=head2 method append
5484
55-
=head1 Methods
85+
method append(*@)
86+
87+
Warns the user that they tried to append onto a C<Nil>.
5688
5789
=head2 method gist
5890
@@ -62,9 +94,45 @@ Returns C<"Nil">.
6294
6395
=head2 method Str
6496
65-
method Str() returns Str:D
97+
method Str()
98+
99+
Warns the user that they tried to stringify a C<Nil>.
100+
101+
=head2 method new
102+
103+
method new (*@)
104+
105+
Returns C<Nil>
106+
107+
=head2 method prepend
108+
109+
method prepend(*@)
110+
111+
Warns the user that they tried to prepend onto a C<Nil>.
112+
113+
=head2 method push
114+
115+
method push(*@)
116+
117+
Warns the user that they tried to push onto a C<Nil>.
118+
119+
=head2 method unshift
120+
121+
method unshift(*@)
122+
123+
Warns the user that they tried to unshift onto a C<Nil>.
124+
125+
=head2 method new
126+
127+
method new()
128+
129+
Returns C<Nil>.
130+
131+
=head2 method Numeric
132+
133+
method Numeric()
66134
67-
Returns the empty string.
135+
Warns the user that they tried to numify a C<Nil>.
68136
69137
=end pod
70138

0 commit comments

Comments
 (0)