September 2017
Beginner
402 pages
9h 52m
English
In the previous examples, the type of the content that is hosted in a variable container was defined by the value that was assigned to a variable:
my $x; # Declaring a variable as a container.$x = 2; # Now it contains an integer.$x = 'Two'; # But now it keeps a string.
Perl 6 allows you to make the type of the variable container strict by specifying it together with a variable declaration:
my Int $x = 2;
Here, the $x variable will only be able to accept integers. An attempt to assign it to a string, for example, will result in the following error:
$x = 'Two'; # Type check failed in assignment to $x; # expected Int but got Str ("Two")
Similarly, Perl 6 allows elements of different types in the same array:
my @a = (1, 'two', ...