Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Variables: type constraints, default values.
Kristien++ pointed out the lack of docs
  • Loading branch information
moritz committed Jan 24, 2015
1 parent 87bdadc commit ea35d8e
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/Language/variables.pod
Expand Up @@ -451,6 +451,30 @@ say $cookies; # -> 6
Like the L<C<temp>|#The_temp_Listop> listop, but only restores the
value if the block exits unsuccessfully.
=head1 Type Constraints and Initialization
Variables can have a type constraint, which goes between the declarator and
the variable name:
=begin code :allow<L>
my Int $x = 42;
$x = 'a string'; # throws an L<X::TypeCheck::Assignment|/type/X::TypeCheck::Assignment> error
=end code
If a scalar variable has a type constraint but no initial value, it is initialized
with the type object of the constraint type.
my Int $x;
say $x.^name; # Int
say $x.defined; # False
Scalar variables without an explicit type constraint are typed as
L<Mu|/type/Mu>, but default to the L<Any|/type/Any> type object.
Variables with the C<@> sigil are initialized with an empty
L<Array|/type/Array>, Variables with the C<%> sigil are initialized with an
empt L<Hash|/type/Hash>.
=head1 Special Variables
Perl 5 is infamous for its many obscure special variables. Perl 6 also
Expand Down

0 comments on commit ea35d8e

Please sign in to comment.