develooper Front page | perl.perl6.users | Postings from April 2018

inheritance and default attributes

Thread Next
From:
Theo van den Heuvel
Date:
April 30, 2018 08:55
Subject:
inheritance and default attributes
Message ID:
2a2b79496bbe32ef691c746d47567d78@heuvelhlt.nl
Hi all,

trying to make sense of the following excerpt from the documentation on 
object construction:

  Due to the default behavior of BUILDALL and BUILD submethods, named 
arguments to the constructor new derived from Mu
  can correspond directly to public attributes of any of the classes in 
the method resolution order, or to any named
  parameter of any BUILD submethod.


I would like to build a class A and subclasses AB and more such that AB 
and friends differ in the default attribute values. On the basis of the 
docs I expected this to work:

#<code>
class A {
   has Int $.a;
   method sayit() { say $!a }
}

class AB is A {
   submethod BUILD(:$!a = 17){}
}


my $ab = AB.new().sayit;
#</code>

however. I get "Attribute $!a not declared in class AB", which makes 
sense.
How should I write this instead?

Thanks


-- 
Theo van den Heuvel
Van den Heuvel HLT Consultancy

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About