Home > Perl6 > Empty is considered useful

Empty is considered useful

I found myself having to convert the arguments of a sub with plenty of optional named arguments into a hash to be passed on into another function.

sub f(:a?, :b?){
    my %h = 
        $a ?? a=>$a !! Emtpy, 
        $b ?? b=>$b !! Emtpy;
}

dd f(b=>1); # Hash %h = {:b(1)}

As it turns out, Empty is skipped when the list of Pairs is turned into a Hash. The short form of Emtpy is |(), a Slip of the empty list. Unless you love Lisp, that may be a little hard to read.

Advertisements
Categories: Perl6
  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: