-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.html
765 lines (673 loc) · 31 KB
/
README.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
<!DOCTYPE html>
<html lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>README</title>
<meta charset="UTF-8">
<style>hr,
img {
box-sizing: content-box
}
body::after,
body::before,
hr::after,
hr::before {
display: table;
content: ""
}
a,
a:not([href]) {
text-decoration: none
}
hr,
svg:not(:root) {
overflow: hidden
}
img,
table tr {
background-color: #fff
}
pre,
table {
overflow: auto
}
dl,
dl dt,
hr,
pre code,
pre>code,
td,
th {
padding: 0
}
input,
pre code {
overflow: visible
}
pre,
pre code {
word-wrap: normal
}
body {
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
color: #333;
font-family: "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
font-size: 16px;
line-height: 1.5;
word-wrap: break-word;
width: 820px;
margin: 2em auto;
}
a {
background-color: transparent;
-webkit-text-decoration-skip: objects;
color: #4078c0
}
a:active,
a:hover {
outline-width: 0;
text-decoration: underline
}
h1 {
margin: .67em 0
}
img {
border-style: none;
max-width: 100%
}
h1,
h2 {
padding-bottom: .3em;
border-bottom: 1px solid #eee
}
input {
font: inherit;
margin: 0;
font-family: inherit;
font-size: inherit;
line-height: inherit
}
* {
box-sizing: border-box
}
strong {
font-weight: 600
}
body::after,
hr::after {
clear: both
}
table {
border-spacing: 0;
border-collapse: collapse;
display: block;
width: 100%
}
blockquote {
margin: 0;
padding: 0 1em;
color: #777;
border-left: .25em solid #ddd
}
ol ol,
ul ol {
list-style-type: lower-roman
}
ol ol ol,
ol ul ol,
ul ol ol,
ul ul ol {
list-style-type: lower-alpha
}
dd {
margin-left: 0
}
code {
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace
}
pre {
font: 12px Consolas, "Liberation Mono", Menlo, Courier, monospace
}
input {
-webkit-font-feature-settings: "liga" 0;
font-feature-settings: "liga" 0
}
body>:first-child {
margin-top: 0!important
}
body>:last-child {
margin-bottom: 0!important
}
a:not([href]) {
color: inherit
}
blockquote,
dl,
ol,
p,
pre,
table,
ul {
margin-top: 0;
margin-bottom: 16px
}
hr {
background: #e7e7e7;
height: .25em;
margin: 24px 0;
border: 0
}
blockquote>:first-child {
margin-top: 0
}
blockquote>:last-child {
margin-bottom: 0
}
h1,
h2,
h3,
h4,
h5,
h6 {
margin-top: 24px;
margin-bottom: 16px;
font-weight: 600;
line-height: 1.25
}
dl dt,
table th {
font-weight: 700
}
h1 code,
h1 tt,
h2 code,
h2 tt,
h3 code,
h3 tt,
h4 code,
h4 tt,
h5 code,
h5 tt,
h6 code,
h6 tt {
font-size: inherit
}
h1 {
font-size: 2em
}
h2 {
font-size: 1.5em
}
h3 {
font-size: 1.25em
}
h4 {
font-size: 1em
}
h5 {
font-size: .875em
}
h6 {
font-size: .85em;
color: #777
}
ol,
ul {
padding-left: 2em
}
ol ol,
ol ul,
ul ol,
ul ul {
margin-top: 0;
margin-bottom: 0
}
li>p {
margin-top: 16px
}
li+li {
margin-top: .25em
}
dl dt {
margin-top: 16px;
font-size: 1em;
font-style: italic
}
dl dd {
padding: 0 16px;
margin-bottom: 16px
}
table td,
table th {
padding: 6px 13px;
border: 1px solid #ddd
}
table tr {
border-top: 1px solid #ccc
}
table tr:nth-child(2n) {
background-color: #f8f8f8
}
code {
padding: .2em 0;
margin: 0;
font-size: 85%;
background-color: rgba(0, 0, 0, .04);
border-radius: 3px
}
code::after,
code::before {
letter-spacing: -.2em;
content: "\00a0"
}
pre>code {
margin: 0;
font-size: 100%;
word-break: normal;
white-space: pre;
background: 0 0;
border: 0
}
pre {
padding: 16px;
font-size: 85%;
line-height: 1.45;
background-color: #f7f7f7;
border-radius: 3px
}
pre code {
display: inline;
max-width: auto;
margin: 0;
line-height: inherit;
background-color: transparent;
border: 0
}
pre code::after,
pre code::before {
content: normal
}
kbd {
display: inline-block;
padding: 3px 5px;
font: 11px Consolas, "Liberation Mono", Menlo, Courier, monospace;
line-height: 10px;
color: #555;
vertical-align: middle;
background-color: #fcfcfc;
border: 1px solid #ccc;
border-bottom-color: #bbb;
border-radius: 3px;
box-shadow: inset 0 -1px 0 #bbb
}
hr {
border-bottom-color: #eee
}
</style>
<style class="build-tools-gen-doc">.toc-level-1 .toc-text { padding-left: 1.5em; }
.toc-level-2 .toc-text { padding-left: 2.5em; }
.toc-level-3 .toc-text { padding-left: 3.5em; }
.toc-level-4 .toc-text { padding-left: 4.5em; }
.toc-level-5 .toc-text { padding-left: 5.5em; }
#TOC * { border-width: 0; }
li > p { margin: inherit; }
li > .pod-block-code { margin-top: 16px; }
</style></head>
<body class="pod">
<div id="___top"></div>
<nav class="indexgroup">
<table id="TOC">
<caption><h2 id="TOC_Title">Table of Contents</h2></caption>
<tr class="toc-level-1"><td class="toc-number">1</td><td class="toc-text"><a href="#NAME">NAME</a></td></tr>
<tr class="toc-level-1"><td class="toc-number">2</td><td class="toc-text"><a href="#SYNOPSIS">SYNOPSIS</a></td></tr>
<tr class="toc-level-1"><td class="toc-number">3</td><td class="toc-text"><a href="#DESCRIPTION">DESCRIPTION</a></td></tr>
<tr class="toc-level-2"><td class="toc-number">3.1</td><td class="toc-text"><a href="#Laziness_for_beginners">Laziness for beginners</a></td></tr>
<tr class="toc-level-3"><td class="toc-number">3.1.1</td><td class="toc-text"><a href='#What_is_"lazy_attribute"'>What is "lazy attribute"</a></td></tr>
<tr class="toc-level-3"><td class="toc-number">3.1.2</td><td class="toc-text"><a href="#When_is_it_useful?">When is it useful?</a></td></tr>
<tr class="toc-level-1"><td class="toc-number">4</td><td class="toc-text"><a href="#USAGE">USAGE</a></td></tr>
<tr class="toc-level-2"><td class="toc-number">4.1</td><td class="toc-text"><a href="#Trait_parameters">Trait parameters</a></td></tr>
<tr class="toc-level-2"><td class="toc-number">4.2</td><td class="toc-text"><a href="#Public/Private">Public/Private</a></td></tr>
<tr class="toc-level-2"><td class="toc-number">4.3</td><td class="toc-text"><a href="#User_method's_(callbacks)_options">User method's (callbacks) options</a></td></tr>
<tr class="toc-level-3"><td class="toc-number">4.3.1</td><td class="toc-text"><a href="#Options">Options</a></td></tr>
<tr class="toc-level-2"><td class="toc-number">4.4</td><td class="toc-text"><a href="#Definite_Types">Definite Types</a></td></tr>
<tr class="toc-level-2"><td class="toc-number">4.5</td><td class="toc-text"><a href="#Some_magic">Some magic</a></td></tr>
<tr class="toc-level-2"><td class="toc-number">4.6</td><td class="toc-text"><a href="#Performance">Performance</a></td></tr>
<tr class="toc-level-2"><td class="toc-number">4.7</td><td class="toc-text"><a href="#Multi-threading">Multi-threading</a></td></tr>
<tr class="toc-level-1"><td class="toc-number">5</td><td class="toc-text"><a href="#CAVEATS">CAVEATS</a></td></tr>
<tr class="toc-level-2"><td class="toc-number">5.1</td><td class="toc-text"><a href="#Cloning">Cloning</a></td></tr>
<tr class="toc-level-1"><td class="toc-number">6</td><td class="toc-text"><a href="#SEE_ALSO">SEE ALSO</a></td></tr>
<tr class="toc-level-1"><td class="toc-number">7</td><td class="toc-text"><a href="#COPYRIGHT">COPYRIGHT</a></td></tr>
<tr class="toc-level-1"><td class="toc-number">8</td><td class="toc-text"><a href="#LICENSE">LICENSE</a></td></tr>
</table>
</nav>
<div class="pod-body">
<h1 id="NAME"><a class="u" href="#___top" title="go to top of document">NAME</a></h1>
<p><code>AttrX::Mooish</code> - extend attributes with ideas from Moo/Moose (laziness!)</p>
<h1 id="SYNOPSIS"><a class="u" href="#___top" title="go to top of document">SYNOPSIS</a></h1>
<pre class="pod-block-code">use AttrX::Mooish;
class Foo {
has $.bar1 is mooish(:lazy, :clearer, :predicate) is rw;
has $!bar2 is mooish(:lazy, :clearer, :predicate, :trigger);
has Num $.bar3 is rw is mooish(:lazy, :filter);
method build-bar1 {
"lazy init value"
}
method !build-bar2 {
"this is private mana!"
}
method !trigger-bar2 ( $value ) {
# do something after attribute changed.
}
method build-bar3 {
rand;
}
method filter-bar3 ( $value, *%params ) {
if %params<old-value>:exists {
# Only allow the value to grow
return ( !%params<old-value>.defined || $value > %params<old-value> ) ?? $value !! %params<old-value>;
}
# Only allow inital values from 0.5 and higher
return $value < 0.5 ?? Nil !! $value;
}
method baz {
# Yes, works with private too! Isn't it magical? ;)
"Take a look at the magic: «{ $!bar2 }»";
}
}
my $foo = Foo.new;
say $foo.bar1;
say $foo.bar3.defined ?? "DEF" !! "UNDEF";
for 1..10 { $foo.bar3 = rand; say $foo.bar3 }</pre>
<p>The above would generate a output similar to the following:</p>
<pre class="pod-block-code">lazy init value
UNDEF
0.08662089602505263
0.49049512098324255
0.49049512098324255
0.5983833081770437
0.9367804461546302
0.9367804461546302
0.9367804461546302
0.9367804461546302
0.9367804461546302
0.9367804461546302</pre>
<h1 id="DESCRIPTION"><a class="u" href="#___top" title="go to top of document">DESCRIPTION</a></h1>
<p>This module is aiming at providing some functionality we're all missing from Moo/Moose. It implements laziness, accompanying methods and adds attribute value filter on top of what standard Moo/Moose provide.</p>
<p>What makes this module different from previous versions one could find in the Raku modules repository is that it implements true laziness allowing <em>Nil</em> to be a first-class value of a lazy attribute. In other words, if you look at the <a href="#SYNOPSIS">SYNOPSIS</a> section, <code>$.bar3</code> value could randomly be either undefined or 3.1415926.</p>
<h2 id="Laziness_for_beginners"><a class="u" href="#___top" title="go to top of document">Laziness for beginners</a></h2>
<p>This section is inteded for beginners and could be skipped by experienced lazybones.</p>
<h3 id='What_is_"lazy_attribute"'><a class="u" href="#___top" title="go to top of document">What is "lazy attribute"</a></h3>
<p>As always, more information could be found by Google. In few simple words: a lazy attribute is the one which gets its first value on demand, i.e. – on first read operation. Consider the following code:</p>
<pre class="pod-block-code">class Foo {
has $.bar is mooish(:lazy, :predicate);
method build-bar { π }
}
my $foo = Foo.new
say $foo.has-bar; # False
say $foo.bar; # 3.1415926...
say $foo.has-bar; # True</pre>
<h3 id="When_is_it_useful?"><a class="u" href="#___top" title="go to top of document">When is it useful?</a></h3>
<p>Laziness becomes very handy in cases where intializing an attribute is very expensive operation yet it is not certain if attribute is gonna be used later or not. For example, imagine a monitoring code which raises an alert when a failure is detected:</p>
<pre class="pod-block-code">class Monitor {
has $.notifier;
has $!failed-object;
submethod BUILD {
$!notifier = Notifier.new;
}
method report-failure {
$.notifier.alert( :$!failed-object );
}
...
}</pre>
<p>Now, imagine that notifier is a memory-consuming object, which is capable of sending notification over different kinds of media (SMTP, SMS, messengers, etc...). Besides, preparing handlers for all those media takes time. Yet, failures are rare and we may need the object, say, once in 10000 times. So, here is the solution:</p>
<pre class="pod-block-code">class Monitor {
has $.notifier is mooish(:lazy);
has $!failed-object;
method build-notifier { Notifier.new( :$!failed-object ) }
method report-failure {
$.notifier.alert;
}
...
}</pre>
<p>Now, it would only be created when we really need it.</p>
<p>Such approach also works well in interactive code where many wuch objects are created only the moment a user action requires them. This way overall responsiveness of a program could be significally incresed so that instead of waiting long once a user would experience many short delays which sometimes are even hard to impossible to be aware of.</p>
<p>Laziness has another interesting application in the area of taking care of attribute dependency. Say, <code>$.bar1</code> value depend on <code>$.bar2</code>, which, in turn, depends either on <code>$.bar3</code> or <code>$.bar4</code>. In this case instead of manually defining the order of initialization in a <code>BUILD</code> submethod, we just have the following code in our attribute builders:</p>
<pre class="pod-block-code">method build-bar2 {
if $some-condition {
return self.prepare( $.bar3 );
}
self.prepare( $.bar4 );
}</pre>
<p>This module would take care of the rest.</p>
<h1 id="USAGE"><a class="u" href="#___top" title="go to top of document">USAGE</a></h1>
<p>The <a href="#SYNOPSIS">SYNOPSIS</a> is a very good example of how to use the trait <code>mooish</code>.</p>
<h2 id="Trait_parameters"><a class="u" href="#___top" title="go to top of document">Trait parameters</a></h2>
<ul><li><p><em><code>lazy</code></em></p>
<p><code>Bool</code>, defines wether attribute is lazy. Can have <code>Bool</code>, <code>Str</code>, or <code>Callable</code> value. The later two have the same meaning, as for <em><code>builder</code></em> parameter.</p>
</li>
<li><p><em><code>builder</code></em></p>
<p>Defines builder method for a lazy attribute. The value returned by the method will be used to initialize the attribute.</p>
<p>This parameter can have <code>Str</code> or <code>Callable</code> values or be not defined at all. In the latter case we expect a method with a name composed of "<em>build-</em>" prefix followed by attribute name to be defined in our class. For example, for a attribute named <code>$!bar</code> the method name is expected to be <em>build-bar</em>.</p>
<p>A string value defines builder's method name.</p>
<p>A callable value is used as-is and invoked as an object method. For example:</p>
<pre class="pod-block-code">class Foo {
has $.bar is mooish(:lazy, :builder( -> $,*% {"in-place"} );
}
$inst = Foo.new;
say $inst.bar;</pre>
<p>This would output '<em>in-place</em>'.</p>
<p>*Note* the use of slurpy <code>*%</code> in the pointy block. Read about callback parameters below.</p>
</li>
<li><p><em><code>predicate</code></em></p>
<p>Could be <code>Bool</code> or <code>Str</code>. When defined trait will add a method to determine if attribute is set or not. Note that it doesn't matter wether it was set with a builder or by an assignment.</p>
<p>If parameter is <code>Bool</code> <em>True</em> then method name is made of attribute name prefixed with <u>has-</u>. See <a href="#What_is_%26quot%3Blazy_attribute%26quot%3B">What is "lazy attribute"</a> section for example.</p>
<p>If parameter is <code>Str</code> then the string contains predicate method name:</p>
<pre class="pod-block-code"> has $.bar is mooish(:lazy, :predicate<bar-is-ready>);
...
method baz {
if self.bar-is-ready {
...
}
}
</pre>
</li>
<li><p><em><code>clearer</code></em></p>
<p>Could be <code>Bool</code> or <code>Str</code>. When defined trait will add a method to reset the attribute to uninitialzed state. This is not equivalent to <em>undefined</em> because, as was stated above, <em>Nil</em> is a valid value of initialized attribute.</p>
<p>Similarly to <em><code>predicate</code></em>, when <em>True</em> the method name is formed with <u>clear-</u> prefix followed by attribute's name. A <code>Str</code> value defines method name:</p>
<pre class="pod-block-code"> has $.bar is mooish(:lazy, :clearer<reset-bar>, :predicate);
...
method baz {
$.bar = "a value";
say self.has-bar; # True
self.reset-bar;
say self.has-bar; # False
}
</pre>
</li>
<li><p><em><code>filter</code></em></p>
<p>A filter is a method which is executed right before storing a value to an attribute. What is returned by the method will actually be stored into the attribute. This allows us to manipulate with a user-supplied value in any necessary way.</p>
<p>The parameter can have values of <code>Bool</code>, <code>Str</code>, <code>Callable</code>. All values are treated similarly to the <code>builder</code> parameter except that prefix '<em>filter-</em>' is used when value is <em>True</em>.</p>
<p>The filter method is passed with user-supplied value and the following named parameters:</p>
<p><code>attribute</code> - contains full attribute name.</p>
<p><code>builder</code> - passed if filter is called as a stage of attribute building.</p>
<p><code>old-value</code> - passed with the previous attribute value if it had one; i.e. if attribute has been initialized.</p>
<p><strong>Note</strong> that it is not recommended for a filter method to use the corresponding attribute directly as it may cause unforseen side-effects like deep recursion. The <code>old-value</code> parameter is the right way to do it.</p>
</li>
<li><p><em><code>trigger</code></em></p>
<p>A trigger is a method which is executed right after attribute value has been changed.</p>
<p>Allowed values for this parameter are <code>Bool</code>, <code>Str</code>, <code>Callable</code>. All values are treated similarly to the <code>builder</code> parameter except that prefix '<em>trigger-</em>' is used when value is <em>True</em>.</p>
<p>Trigger method gets passed with the stored value as first positional parameter. If there is also a <code>filter</code> defined for the attribute then trigger receives the value returned by the filter, not the initial. I.e. it always get what's eventually stored in the attribute. It also receives the same named parameters as <code>filter</code> method: <code>attribute</code>, <code>builder</code>, <code>old-value</code>.</p>
</li>
<li><p><em><code>alias</code>, <code>aliases</code>, <code>init-arg</code>, <code>init-args</code></em></p>
<p>Those are four different names for the same parameter which allows defining attribute aliases. So, whereas Internally you would have single container for an attribute that container would be accessible via different names. And it means not only attribute accessors but also clearer and predicate methods:</p>
<pre class="pod-block-code">class Foo {
has $.bar is rw is mooish(:clearer, :lazy, :aliases<fubar baz>);
method build-bar { "The Answer" }
}
my $inst = Foo.new( fubar => 42 );
say $inst.bar; # 42
$inst.clear-baz;
say $inst.bar; # The Answer
$inst.fubar = pi;
say $inst.baz; # 3.1415926</pre>
<p>Aliases are not applicable to methods called by the module like builders, triggers, etc.</p>
</li>
<li><p><em><code>no-init</code></em></p>
<p>This parameter will prevent the attribute from being initialized by the constructor:</p>
<pre class="pod-block-code">class Foo {
has $.bar is mooish(:lazy, :no-init);
method build-bar { 42 }
}
my $inst = Foo.new( bar => "wrong answer" );
note $inst.bar; # 42</pre>
</li>
<li><p><em><code>composer</code></em></p>
<p>This is a very specific option mostly useful until role <code>COMPOSE</code> phaser is implemented. Method of this option is called upon class composition time.</p>
</li>
</ul>
<h2 id="Public/Private"><a class="u" href="#___top" title="go to top of document">Public/Private</a></h2>
<p>For all the trait parameters, if it is applied to a private attribute then all auto-generated methods will be private too.</p>
<p>The call-back style options such as <code>builder</code>, <code>trigger</code>, <code>filter</code> are expected to share the privace mode of their respective attribute:</p>
<pre class="pod-block-code"> class Foo {
has $!bar is rw is mooish(:lazy, :clearer<reset-bar>, :predicate, :filter<wrap-filter>);
method !build-bar { "a private value" }
method baz {
if self!has-bar {
self!reset-bar;
}
}
method !wrap-filter ( $value, :$attribute ) {
"filtered $attribute: ($value)"
}
}
</pre>
<p>Though if a callback option is defined with method name instead of <code>Bool</code> <em>True</em> then if method wit the same privacy mode is not found then opposite mode would be tried before failing:</p>
<pre class="pod-block-code"> class Foo {
has $.bar is mooish( :trigger<on_change> );
has $!baz is mooish( :trigger<on_change> );
has $!fubar is mooish( :lazy<set-fubar> );
method !on_change ( $val ) { say "changed! ({$val})"; }
method set-baz { $!baz = "new pvt" }
method use-fubar { $!fubar }
}
$inst = Foo.new;
$inst.bar = "new"; # changed! (new)
$inst.set-baz; # changed! (new pvt)
$inst.use-fubar; # Dies with "No such private method '!set-fubar' for invocant of type 'Foo'" message
</pre>
<h2 id="User_method's_(callbacks)_options"><a class="u" href="#___top" title="go to top of document">User method's (callbacks) options</a></h2>
<p>User defined (callback-type) methods receive additional named parameters (options) to help them understand their context. For example, a class might have a couple of attributes for which it's ok to have same trigger method if only it knows what attribute it is applied to:</p>
<pre class="pod-block-code"> class Foo {
has $.foo is rw is mooish(:trigger('on_fubar'));
has $.bar is rw is mooish(:trigger('on_fubar'));
method on_fubar ( $value, *%opt ) {
say "Triggered for {%opt<attribute>} with {$value}";
}
}
my $inst = Foo.new;
$inst.foo = "ABC";
$inst.bar = "123";
</pre>
<pre class="pod-block-code">The expected output would be:</pre>
<pre class="pod-block-code"> Triggered for $!foo with with ABC
Triggered for $!bar with with 123
</pre>
<p><strong>NOTE:</strong> If a method doesn't care about named parameters it may only have positional arguments in its signature. This doesn't work for pointy blocks where anonymous slurpy hash would be required:</p>
<pre class="pod-block-code"> class Foo {
has $.bar is rw is mooish(:trigger(-> $, $val, *% {...}));
}
</pre>
<h3 id="Options"><a class="u" href="#___top" title="go to top of document">Options</a></h3>
<ul><li><p><em><code>attribute</code></em></p>
<p>Full attribute name with twigil. Passed to all callbacks.</p>
</li>
<li><p><em><code>builder</code></em></p>
<p>Only set to <em>True</em> for <code>filter</code> and <code>trigger</code> methods when attribute value is generated by lazy builder. Otherwise no this parameter is not passed to the method.</p>
</li>
<li><p><em><code>old-value</code></em></p>
<p>Set for <code>filter</code> only. See its description above.</p>
</li>
</ul>
<h2 id="Definite_Types"><a class="u" href="#___top" title="go to top of document">Definite Types</a></h2>
<p>The module allows for lazy attributes to have a definite type. By default the compiler will throw an error unless such attribute has `is required` trait or a default value. But for lazy attributes the default value must be provided by their builders:</p>
<pre class="pod-block-code">class Foo {
has Int:D $.the-answer is mooish(:lazy);
method build-the-answer { 42 }
}</pre>
<p>Apparently, the value returned by a builder must pass the typecheck. Therefore the following code will fail a run time:</p>
<pre class="pod-block-code">class Foo {
has Str:D $.bad is mooish(:lazy);
method build-bad { Str }
}</pre>
<h2 id="Some_magic"><a class="u" href="#___top" title="go to top of document">Some magic</a></h2>
<p>Note that use of this trait doesn't change attribute accessors. More than that, accessors are not required for private attributes. Consider the <code>$!bar2</code> attribute from <a href="#SYNOPSIS">SYNOPSIS</a>.</p>
<h2 id="Performance"><a class="u" href="#___top" title="go to top of document">Performance</a></h2>
<p>Module versions prior to v0.5.0 were pretty much costly perfomance-wise. This was happening due to use of <a href="https://docs.raku.org/type/Proxy"><code>Proxy</code></a> to handle all attribute read/writes. Since v0.5.0 only the first read/write operation would be handled by this module unless <code>filter</code> or <code>trigger</code> parameters are used. When <code>AttrX::Mooish</code> is assured that the attribute is properly initialized it steps aside and lets the Raku core to do its job without intervention.</p>
<p>The only exception takes place if <code>clearer</code> parameter is used and <code>clear-<attribute></code> method is called. In this case the attribute state is reverted back to uninitialized state and <a href="https://docs.raku.org/type/Proxy"><code>Proxy</code></a> is getting installed again – until the next read/write operation.</p>
<p><code>filter</code> and <code>trigger</code> are special with this respect because they require permanent monitoring of attribute operations making it effectively impossible to strip off <code>Proxy</code> from attribute's value. For this reason use of these parameters must be very carefully considered. One is highly discouraged from using them for any code where performance is important.</p>
<h2 id="Multi-threading"><a class="u" href="#___top" title="go to top of document">Multi-threading</a></h2>
<p>This module provides partial thread-safety and must be used with care with this respect. This means that the following conditions are guaranteed:</p>
<ul><li><p>build operations are safe among themselves</p>
</li>
<li><p>clear operations are safe among themselves</p>
</li>
<li><p>anything else, including mix of build/clear operations, is unsafe</p>
</li>
</ul>
<p>Consider it the way we normally consider working with an attribute in a concurrent environment, where reads and writes must be mutually protected to ensure data safety.</p>
<p>To sum up the above stated, what would be guaranteed is that a read-only attribute would provide robust results in a multi-threaded environment, as it is expected from a read-only pre-initialized attribute.</p>
<p>Predicates are considered <em>read</em> operations and as such are not protected either. Think of testing a non-mooified attribute for definedness, for example.</p>
<h1 id="CAVEATS"><a class="u" href="#___top" title="go to top of document">CAVEATS</a></h1>
<ul><li><p>Due to the "magical" nature of attribute behaviour conflicts with other traits are possible. In particular, mixing up with `is built` trait is not recommended.</p>
</li>
<li><p>Use of <a href="https://docs.raku.org/type/Proxy"><code>Proxy</code></a> as the container may have unexpected side effects in some use cases like passing it as a parameter. Multiple calls of <code>Proxy</code>'s <code>FETCH</code> are possible, for example. While generally harmless this may result in performance issues of affected application. To workaround the problem attribute value can be temporarily assigned into a variable.</p>
</li>
<li><p>Another surprising side effect happens when a "mooified" array or hash attribute is used with a loop. Since <a href="https://docs.raku.org/type/Proxy"><code>Proxy</code></a> is a container, loops are considering such attributes as itemized, no matter what their final value is. Consider the following:</p>
<pre class="pod-block-code">class Foo {
has @.a is mooish(:lazy);
method build-a { 1,2,3 }
method dump {
for @!a -> $val { say $val.raku }
}
}
Foo.new.dump; # $[1, 2, 3]</pre>
<p>Note that this only happens when attribute is accessed privately as <code>for Foo.new.a {...}</code> would behave as expected. Also, for non-filtering and non-triggering attributes this only happens when the attribute is not initialized yet.</p>
<p>The problem could be workarounded either by using <code>@.a</code> notation, or with explicit decontainerization <code>@!a<></code>.</p>
</li>
</ul>
<h2 id="Cloning"><a class="u" href="#___top" title="go to top of document">Cloning</a></h2>
<p>Cloning of an object with mooified attributes is a non-trivial case resulting from the use of <a href="https://docs.raku.org/type/Proxy"><code>Proxy</code></a>. Again. The root of the problem lies in the fact that <code>Proxy</code> is using closures for its <code>FETCH</code>/<code>STORE</code>. This is how it knows what object is to be updated when necessary: the object is captured by the closures. But what help us under normal circumstances becomes our problem after cloning because the new object attributes would still be bound to proxies referring the original instance!</p>
<p><code>AttrX::Mooish</code> handles this situation starting with v1.0.0 release by implementing post-clone fix up procedure where all proxies are getting replaced using new closures. This is implemented by installing special <code>clone</code> method into each class containing mooified attributes.</p>
<p>So far, so good until it comes down to lazy attributes which were already initialized using other attributes of the class. Simple cloning doesn't affect them, but if their dependency has been changed using twiddles then we're in trouble:</p>
<pre class="pod-block-code">class Foo {
has $.a1 = 1;
has $.a2 is mooish(:lazy);
method build-a2 { $!a1 * 2 }
}
my $obj = Foo.new;
say $obj.a2; # 2
my $copy = $obj.clone(:a1(3));
say $copy.a2; # Oops, it's still 2!</pre>
<p>In such situations <code>AttrX::Mooish</code> resets the lazy attributes to unitialized state so they would get re-initialized again using new values.</p>
<p>The only exception is for writable (<code>is rw</code>) lazy attributes. When they hold a value there is no way for us to know where the value came from because it could have been assigned by code external to our class in which case resetting it might be not so smart.</p>
<p><strong>IMPORTTANT!</strong> Also remember that a non-writable attribute can still be assigned via private accessor (<code>$!attr = $value;</code>) by class code. These cases cannot be detected either but disreprected by the module. Taking care of them is developer's responsibility!</p>
<p>Generally speaking, there is no good strategy to handle all possible cases what it comes to cloning lazy entities. Sometimes it would be better to resolve edge cases manually. If this is your case then metamodel method `post-clone` can be used to do the standard fixup job:</p>
<pre class="pod-block-code">class Foo {
...
method clone(*%twiddles) {
my \cloned = ::?CLASS.^post-clone: self, callsame(), %twiddles;
... # Do specific fixups here
cloned
}
}</pre>
<p>Basically, the first line is what <code>AttrX::Mooish</code> installs for you by default. Consider the use of <code>::?CLASS</code> instead of <code>self</code>. This is mandatory because <code>self</code> can be an instance of a child class.</p>
<h1 id="SEE_ALSO"><a class="u" href="#___top" title="go to top of document">SEE ALSO</a></h1>
<p><a href="ChangeLog.html">ChangeLog</a></p>
<h1 id="COPYRIGHT"><a class="u" href="#___top" title="go to top of document">COPYRIGHT</a></h1>
<p>(c) 2023, Vadim Belman <[email protected]></p>
<h1 id="LICENSE"><a class="u" href="#___top" title="go to top of document">LICENSE</a></h1>
<p>Artistic License 2.0</p>
<p>See the <a href="LICENSE"><em>LICENSE</em></a> file in this distribution.</p>
</div>
</body>
</html>