Skip to content

Commit e3f77d9

Browse files
committed
Added class attributes
1 parent 99de2fe commit e3f77d9

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

lib/Language/classtut.pod

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,37 @@ X<|traits, is rw>
166166
The C<is rw> trait causes the generated accessor method to return something
167167
external code can modify to change the value of the attribute.
168168
169+
=head1 Static fields?
170+
171+
Perl6 has no B<static> keyword. Anyway any class may declare anything that
172+
module can so making a scoped variable sounds like good idea.
173+
174+
class Singleton {
175+
176+
my Singleton $instance;
177+
method new {!!!} # Singleton.new dies.
178+
submethod instance {
179+
$instance = Singleton.bless unless $instance;
180+
$instance;
181+
}
182+
}
183+
184+
Class attributes defined by L<my> or L<our> may also be initialized while
185+
declaration but we are implementing Singleton pattern here and object must
186+
be created during first use. It is not possible to predict in 100% the moment
187+
when attribute initialization will be executed because it can be done during
188+
compilation, runtime or both especially when importing class using L<use>
189+
keyword.
190+
191+
class HaveStaticAttr {
192+
my Foo $.foo = some_complicated_subroutine;
193+
}
194+
195+
Class attributes may also be declared with secondary sigil like object
196+
attributes that will result in generating read-only accessor if attribute
197+
is to be public.
198+
199+
169200
=head1 Methods
170201
171202
X<|methods>

0 commit comments

Comments
 (0)