Skip to content

Commit 681795c

Browse files
committed
Try fleshing out the classes section a little.
1 parent 97dabe1 commit 681795c

File tree

1 file changed

+109
-2
lines changed

1 file changed

+109
-2
lines changed

lib/objects.pod

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ with a colon:
3333
say @*INC.join: ':';
3434
3535
Many operations that don't look like method calls (for example smart
36-
matching, interpolating an object into a string) results in method
36+
matching, interpolating an object into a string) result in method
3737
calls under the hood.
3838
3939
Methods can return mutable containers, in which case you can assign
@@ -76,7 +76,114 @@ Subtype checking is done by smart-matching:
7676
7777
=head1 Classes
7878
79-
TODO: attributes, methods, object construction, inheritance
79+
Classes are declared using the C<class> keyword, typically followed by a
80+
name.
81+
82+
class Journey {
83+
}
84+
85+
This declaration results in a type object being created and installed in
86+
the current package and current lexical scope under the name C<Journey>.
87+
You can also declare classes lexically:
88+
89+
my class Journey {
90+
}
91+
92+
This restricts their visibility to the current lexical scope, which can
93+
be useful if the class is an implementation detail nested inside a module
94+
or another class.
95+
96+
=head2 Attributes
97+
98+
Attributes are variables that exist per instance of a class. They are where
99+
the state of an object is stored. In Perl 6, all attributes are private. They
100+
are typically declared using the C<has> declarator and using the C<!> twigil.
101+
102+
class Journey {
103+
has $!origin;
104+
has $!destination;
105+
has @!travellers;
106+
has $!notes;
107+
}
108+
109+
While there is no such thing as a public (or even protected) attribute, there
110+
is a way to have accessor methods generated automatically: replace the C<!>
111+
twigil with the C<.> twigil (the C<.> should remind you of method call).
112+
113+
class Journey {
114+
has $.origin;
115+
has $.destination;
116+
has @!travellers;
117+
has $.notes;
118+
}
119+
120+
This defaults to providing a read-only accessor. In order to allow changes to
121+
the attribute, add the C<rw> trait:
122+
123+
class Journey {
124+
has $.origin;
125+
has $.destination;
126+
has @!travellers;
127+
has $.notes is rw;
128+
}
129+
130+
Since classes inherit a default constructor from C<Mu> and we have requested
131+
that some accessor methods are generated for us, our class is already somewhat
132+
functional.
133+
134+
# Create a new instance of the class.
135+
my $vacation = Journey.new(
136+
origin => 'Sweden',
137+
destination => 'Switzerland',
138+
notes => 'Pack hiking gear!'
139+
);
140+
141+
# Use an accessor; this outputs Sweden.
142+
say $vacation.origin;
143+
144+
# Use an rw accessor to change the value.
145+
$vacation.notes = 'Pack hiking gear and sunglasses!';
146+
147+
Note that the default constructor will only set attributes that have an
148+
accessor method.
149+
150+
=head2 Methods
151+
152+
Methods are declared with the C<method> keyword inside of a class body.
153+
154+
class Journey {
155+
has $.origin;
156+
has $.destination;
157+
has @!travellers;
158+
has $.notes is rw;
159+
160+
method add_traveller($name) {
161+
if $name ne any(@!travellers) {
162+
push @!travellers, $name;
163+
}
164+
else {
165+
warn "$name is already going on the journey!";
166+
}
167+
}
168+
169+
method describe() {
170+
"From $!origin to $!destination"
171+
}
172+
}
173+
174+
A method can have a signature, just like a subroutine. Attributes can be used
175+
in methods, and can always be used with the C<!> twigil, even if they are
176+
declared with the C<.> twigil. This is because really, the C<.> twigil declares
177+
an attribute with the C<!> twigil in its place, and then additionally generates
178+
an accessor method.
179+
180+
There is a subtle but important difference between using, say, C<$!origin> and
181+
C<$.origin> in the method C<describe>. The first is always a simple lookup of
182+
the attribute. It is cheap, and you know that it is the attribute declared in
183+
this class. The latter is really a method call, and thus it may be overridden
184+
in a subclass. Only use C<$.origin> if you explicitly want to allow overriding.
185+
186+
TODO: self, private methods, object construction, inheritance
80187
81188
=head1 Roles
82189

0 commit comments

Comments
 (0)