Permalink
Browse files

explain why named attributes cannot be set from .new by default, and …

…how to make it work anyway
  • Loading branch information...
1 parent ae47fda commit 047e157f26b04ec3e566744b2f04aaa1beaa232b @moritz moritz committed Jun 15, 2012
Showing with 34 additions and 0 deletions.
  1. +34 −0 answers.md
View
@@ -112,6 +112,40 @@ There are several reasons:
* they allow easy single/plural distinction
* many natural languages use mandatory noun markers, so our brains are built to handle it
+<span id="privattr">
+### Why can't I initialize private attributes from the new method, and how can I fix this?
+
+A code like
+
+ class A {
+ has $!x;
+ method show-x {
+ say $!x;
+ }
+ }
+ A.new(x => 5).show-x;
+
+does not print 5. Private attributes are /private/, which means invisible to
+the outside. If the default constructor could initialize them, they would leak
+into the public API.
+
+If you still want it to work, you can a `submethod BUILD` that initializes
+them:
+
+ class B {
+ has $!x;
+ submethod BUILD(:$!x) { }
+ method show-x {
+ say $!x;
+ }
+ }
+ A.new(x => 5).show-x;
+
+`BUILD` is called by the default constructor (indirectly, see
+http://perlgeek.de/blog-en/perl-6/object-construction-and-initialization.html
+for more details) with all the named arguments that the user passes to the
+constructor. `:$!x` is a named parameter with name `x`, and when called
+with a named argument of name `x`, its value is bound to the attribute `$!x`.
<span id="meta" />
## Meta Questions and Advocacy

0 comments on commit 047e157

Please sign in to comment.