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 1␤␤alive␤»

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 1␤␤Actually thrown at:␤  in sub f at /tmp/DBYRCggtFf line 1␤  in block  at /tmp/DBYRCggtFf line 1␤␤»
Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: