This is probably explained somewhere, but I've been unable to piece it
together in my own investigation. If I have a class:

class Foo {
  has Int $!value;
}

I get the impression that the default constructor would allow me to
say

my Foo $foo .= new(value => 2);

Which seems to me to be a bit odd, since $!value is surely supposed to
be private. In any case, how would I write a custom constructor which
initialises $!value (and possibly many other fields which have to be
initialised based on some constructor parameters)? So far I think it's
something like this:

method new($class: Int $v)
{
  my $self = $class.bless();
  $self!value = $v;
  return $self;
}

Or possibly

method new($class: Int $v)
{
  $class.bless(value => $v);
}

Maybe both are valid. Rakudo didn't seem to understand private
attributes when I tried playing with this, so I have no real idea if
I'm barking in the correct forest.

Reply via email to