use strict;
use warnings;
use feature qw/say/;
use Variable::Declaration;
use Types::Standard '-all';
let $foo = 'hello';
say $foo; # => hello
let Str $str1 = {};
# ERROR: Reference {} did not pass type constraint "Str"
let Str $str2 = 'hello';
$str2 = {};
# ERROR: Reference {} did not pass type constraint "Str"
let ($bar, $baz) = ('hello', 'world');
let (Int $a, Int $b) = 1..2;
sub hello {
let Str $message = shift;
say "HELLO $message";
}
hello('OKINAWA!'); # => HELLO OKINAWA!
hello({});
# ERROR: Reference {} did not pass type constraint "Str"
# `static`
# is equivalent to `state` with type constraint
sub counter {
static $i = 0;
$i++;
}
say counter() for 1..10;
#
# `const`
# is equivalent to `my`
# with type constraint and data lock
#
const $pi = 3.14;
$pi = 3.141519;
# ERROR: Modification of a read-only value attempted
use Variable::Declaration level => 2; # default
use Types::Standard qw/Int/;
{
let Int $i = 'hello';
# ERROR: Value "hello" did not pass type constraint "Int"
}
{
let Int $i = 123;
$i = 'hello';
# ERROR: Value "hello" did not pass type constraint "Int"
}
use Variable::Declaration level => 1;
use Types::Standard qw/Int/;
{
let Int $i = 123;
$i = 'hello'; # *NO* Error
}
{
let Int $i = 'hello';
# ERROR: Value "hello" did not pass type constraint "Int"
}
use Variable::Declaration level => 0;
use Types::Standard qw/Int/;
{
let Int $i = 123;
$i = 'hello'; # *NO* Error
}
{
let Int $i = 'hello';
# *NO* Error
}
#
# FAIL CASE...
#
(let $a)
# eq (;;my $a)
if (let $a = 'hello') { }
for let $a (@list)
Created
March 3, 2018 10:04
-
-
Save kfly8/6df635f41509b1a36d3ae6ae9a5b1118 to your computer and use it in GitHub Desktop.
Variable::Declaration example
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment