The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Variable::Declaration - declare with type constraint

SYNOPSIS

    use Variable::Declaration;
    use Types::Standard '-all';

    # variable declaration
    let $foo;      # is equivalent to `my $foo`
    static $bar;   # is equivalent to `state $bar`
    const $baz;    # is equivalent to `my $baz;dlock($baz)`

    # with type constraint

    # init case
    let Str $foo = {}; # => Reference {} did not pass type constraint "Str"

    # store case
    let Str $foo = 'foo';
    $foo = {}; # => Reference {} did not pass type constraint "Str"

DESCRIPTION

Warning: This module is still new and experimental. The API may change in future versions. The code may be buggy.

Variable::Declaration provides new variable declarations, i.e. let, static, and const.

let is equivalent to my with type constraint. static is equivalent to state with type constraint. const is equivalent to let with data lock.

INTROSPECTION

The function Variable::Declaration::info lets you introspect return values like Variable::Declaration::Info:

    use Variable::Declaration;
    use Types::Standard -types;

    let Str $foo = "HELLO";
    my $vinfo = Variable::Declaration::info \$foo;

    $vinfo->declaration; # let
    $vinfo->type; # Str

LEVEL

You can specify the LEVEL in three stages of checking the specified type:

LEVEL 0 does not check type, LEVEL 1 check type only at initializing variables, LEVEL 2 check type at initializing variables and reassignment. LEVEL 2 is default level.

    # CASE: LEVEL 2 (DEFAULT)
    use Variable::Declaration level => 2;

    let Int $s = 'foo'; # => ERROR!
    let Int $s = 123;
    $s = 'bar'; # => ERROR!

    # CASE: LEVEL 1
    use Variable::Declaration level => 1;

    let Int $s = 'foo'; # => ERROR!
    let Int $s = 123;
    $s = 'bar'; # => NO error!

    # CASE: LEVEL 0
    use Variable::Declaration level => 0;

    let Int $s = 'foo'; # => NO error!
    let Int $s = 123;
    $s = 'bar'; # => NO error!

There are three ways of specifying LEVEL. First, as shown in the example above, pass to the arguments of the module. Next, set environment variable $ENV{Variable::Declaration::LEVEL}. Finally, set $Variable::Declaration::DEFAULT_LEVEL.

LICENSE

Copyright (C) kfly8.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

kfly8 <kfly@cpan.org>