Skip to content

Commit ec2fc82

Browse files
committed
Add a few examples for Nil; jnthn++ for the 'evaporation' example
1 parent e792986 commit ec2fc82

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

lib/Type/Nil.pod

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@
99
Represents an empty list in list context, and an undefined value in item
1010
context.
1111
12+
C<Nil>, when assigned, evaporates into the type object of the type of the
13+
container you assigned it into.
14+
15+
my Int $x = 42;
16+
$x = Nil;
17+
$x.say; # (Int)
18+
19+
It is for this reason that assigning an untyped container as C<Nil> will result
20+
in an C<(Any)> object.
21+
22+
my $x = Nil;
23+
$x.say; # (Any)
24+
my Int $y = $x; # error: Type check failed in assignment to '$y'; expected 'Int' but got 'Any'
25+
26+
This is because an untyped container is C<Any> until proven otherwise, and
27+
the C<Nil> evaporates
28+
29+
If you are looking for a variable which transforms objects into type objects
30+
when said variable appears on the RHS, you can type the container as C<Nil>.
31+
32+
my Nil $x;
33+
my Str $s = $x;
34+
$s.say; # (Str)
1235
1336
=head1 Methods
1437

0 commit comments

Comments
 (0)