Home
> Uncategorized > Now they made Array a container!
Now they made Array a container!
What means it comes with a default. The default default happens to be Any
.
my @a; put @a[0].perl; # OUTPUT«Any»
The default type constraint is also Any. So we can happily assign any value to any slot in an Array
. When reading values from a Positional we will get Any
for unassingned slots too, what can be dangerous or cumbersome if we need to check for definedness by hand. In fact, we will have problems to tell if some undefined value was returned (unless we give the Array a type).
my @a is default(Nil); put @a[0]; say "alive"; # OUTPUT«Use of Nil in string context in block at /tmp/_9ikYUP8Qj line 1alive»
Now we get a warning if we turn it into a string and we can test against it. Still it may slip through a crack and corrupt some data.
my @a is default(Failure.new); put @a[0]; say "alive"; # OUTPUT«===SORRY!===Failed»
Now it dies properly. The lack of a stacktrace is a bug that we can work around.
my @a is default(Failure.new('index out of range')); put @a[0]; say "alive"; CATCH { default { put .message; put .backtrace.Str } } # OUTPUT «index out of range in any at src/Perl6/World.nqp line 2004 in any trait_mod:sym at gen/moar/m-Perl6-Actions.nqp line 5060 in any trait_mod:sym at src/Perl6/Grammar.nqp line 3101 in any trait_mod at /home/camelia/rakudo-m-inst-1/sha…»
The same works for any container, including those in signatures.
sub f($s = Failure.new){ say $s }; f; # OUTPUT«Failed in sub f at /tmp/DBYRCggtFf line 1 in block at /tmp/DBYRCggtFf line 1Actually thrown at: in sub f at /tmp/DBYRCggtFf line 1 in block at /tmp/DBYRCggtFf line 1»
Categories: Uncategorized
Comments (0)
Trackbacks (0)
Leave a comment
Trackback