Object::Pad review Yuki Kimoto's 2021-08-21 - Constructor default argument

I start to review Paul Evans's Object::Pad from my personal thinking.

Latest years, Perl core teams positively try to implement Object-Oriented feature to Perl core. I hope my review helps a little.

First time is constructor default arguments.

use Object::Pad;
 
class Point {
   has $x :param = 0;
   has $y :param = 0;
 
   method move {
      my ($dX, $dY) = @_;
      $x += $dX;
      $y += $dY;
   }
 
   method describe {
      print "A point at ($x, $y)\n";
   }
}
 
{
  my $point = Point->new(x => 5, y => 10);
  $point->describe;
}

# Unsupported hash reference
# I think a little time
# Benefit: a little fast because type check isn't done
{
  my $args = {x => 5, y => 10};
  my $point = Point->new($args);
  $point->describe;
}
# Need the following. Not too bad.
# I can accept hash reference argument feature is none.
{
  my $args = {x => 5, y => 10};
  my $point = Point->new(%$args);
  $point->describe;
}

Leave a comment

About Yuki Kimoto

user-pic I'm Perl Programmer. I LOVE Perl. I want to contribute Perl community and Perl users.