(no subject)
Apr. 18th, 2004 06:46 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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
andmethod
rather thanpackage
andsub
.
- 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.
no subject
Date: 2004-04-18 04:59 pm (UTC)no subject
Date: 2004-04-19 06:56 am (UTC)