File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -166,6 +166,37 @@ X<|traits, is rw>
166
166
The C < is rw > trait causes the generated accessor method to return something
167
167
external code can modify to change the value of the attribute.
168
168
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
+
169
200
= head1 Methods
170
201
171
202
X < |methods >
You can’t perform that action at this time.
0 commit comments