Hello 7stud,
I can’t comment on Learning Perl 6 as I haven’t read it, but here is my understanding of the example Point class. Consider the following code which creates a Point object and sets the y attribute:
my Point $p = Point.new; $p.gist.put; $p.set( y => 4 ); $p.gist.put; $p.y.put;
Output:
17:44 >raku 2091_SoPW.raku [0, 0] [0, 4] 4 18:00 >
First, the attributes $.x and $.y are declared with dots rather than exclamation points, meaning that although they are private attributes, Raku will generate read-only getter methods for them. So, in the example above, the method call $p.y returns the value of the attribute $.y.
To set the attributes, it is necessary to provide the class with an explicit set method.
method set( :$x = $.x, :$y = $.y )
Note: Raku allows you to sometimes use $.x as an alias for $!x, but I find that makes things unnecessarily confusing. I think of it this way: the attribute is $!x, but it is declared as $.x to get Raku to provide it with a read-only getter method. So I would prefer to declare the set method as method set( :$x = $!x, :$y = $!y ). But, of course, YMMV.
$!x = $x; $!y = $y;
These statements simply set the object attributes to the values specified as arguments to the set method (or to their default values, if no arguments are specified in the call).
Hope that helps,
| Athanasius <°(((>< contra mundum | סתם עוד האקר של פרל, |
In reply to Re: Raku classes: syntax question
by Athanasius
in thread Raku classes: syntax question
by 7stud
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |