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 )
  1. :$x and :$y declare named parameters x and y, allowing the set method to be called like this: $p.set( x => 3, y => 7 );.
  2. = $.x (or the equivalent = $!x) sets the default value for that parameter. So in the call $p.set( y => 4 ); the x attribute, not being named in the method call, receives its default value, namely $.x, ensuring that the assignment $!x = $x; leaves the x attribute unchanged.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.