2
2
3
3
= TITLE class Nil
4
4
5
- = SUBTITLE Absence of a value
5
+ = SUBTITLE Absence of a value or a benign failure
6
6
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 { }
10
8
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> .
12
13
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 > .
15
15
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.
18
53
19
54
my Int $x = 42;
20
55
$x = Nil;
21
- $x.say; #-> (Int)
56
+ $x.say; #-> (Int)
22
57
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.
25
60
26
61
my $x = Nil;
27
62
$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'
32
65
33
66
If you are looking for a variable which transforms objects into type objects
34
67
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.
45
78
for $x,$y -> $val is rw { $val = $reset unless $val > 0 }
46
79
$x.say; #-> 42
47
80
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
50
82
51
- my @c := 1,Nil,2;
52
- dd @c; # (1, Nil, 2)
53
- say so Nil === any(@c); # True
83
+ = head2 method append
54
84
55
- = head1 Methods
85
+ method append(*@)
86
+
87
+ Warns the user that they tried to append onto a C < Nil > .
56
88
57
89
= head2 method gist
58
90
@@ -62,9 +94,45 @@ Returns C<"Nil">.
62
94
63
95
= head2 method Str
64
96
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()
66
134
67
- Returns the empty string .
135
+ Warns the user that they tried to numify a C < Nil > .
68
136
69
137
= end pod
70
138
0 commit comments