prog: (monkey)
[personal profile] prog
Larry Wall's Apocalypse 12 is out, finally providing Revelations to the shape that objects will take in Perl6. Word on the street is that this is the last “big” Apocalypse, and actual language implementation should actually start to happen RSN. Boy, I hope so. It's been a long wait already, and I wish Larry good health and high spirits as he & the hacksters start making this stuff happen. Peep peep peep, I await with open beak & bulging eyes.

If I may pull a pull-quote:

Before we start talking about all the hard things that should be possible,
let's look at an example of some of the easy things that should be easy.
Suppose we define a Point object that (for some strange reason) allows
you to adjust the y-axis but not the x-axis.



    class Point {
        has $.x;
        has $.y is rw;


        method clear () { $.x = 0; $.y = 0; }
    }


    my $point = Point.new(x => 2, y => 3);


    $a = $point.y;      # okay
    $point.y = 42;      # okay


    $b = $point.x;      # okay
    $point.x = -1;      # illegal, default is read-only


    $point.clear;       # reset to 0,0

If you compare that to how it would have to be written in Perl 5, you'll
note a number of differences:



  • It uses the keywords class and method
    rather than package and sub.

  • The attributes are named in explicit declarations rather than implicit
    hash keys.

  • It is impossible to confuse the attribute variables with ordinary
    variables because of the extra dot (which also associates the attributes
    visually with method calls).

  • Perhaps most importantly, we did not have to commit to using a
    hash (or any other external data structure) for the object's values.

  • We didn't have to write a constructor.

  • The implicit constructor automatically knows how to map named arguments
    to the attribute names.

  • We didn't have to write the accessor methods.

  • The accessors are by default read-only outside the class, and you
    can't get at the attributes from outside the class without an accessor.
    (Inside the class you can use the attributes directly.)

  • The invocant of the clear method is implicit.

  • And perhaps most obviously, Perl 6 uses . instead
    of -> to dereference an object.

August 2022

S M T W T F S
 123456
78910111213
14151617181920
21222324252627
28 293031   

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Aug. 8th, 2025 06:32 am
Powered by Dreamwidth Studios