@@ -297,8 +297,8 @@ from the Perl 6 point of view:
297
297
298
298
= item C < AT-POS > provides a specific element at the given position (starting from zero);
299
299
300
- = item C < list > provides the L < List > of elements within the array building it from the
301
- native array iterator.
300
+ = item C < list > provides the L < List > of elements within the array building
301
+ it from the native array iterator.
302
302
303
303
As an example, consider the following simple piece of code:
304
304
@@ -443,22 +443,20 @@ class AStringAndAnInt is repr("CStruct") {
443
443
has Str $.a_string;
444
444
has int32 $.an_int32;
445
445
446
- submethod TWEAK {
447
- $!a_string := Str.new;
448
- $!an_int32 = 0;
449
- }
450
-
451
446
sub init_struct(AStringAndAnInt is rw, Str, int32) is native('simple-struct') { * }
452
- method init(:$a_string, :$an_int) {
447
+
448
+ submethod BUILD(:$a_string, :$an_int) {
453
449
init_struct(self, $a_string, $an_int);
454
450
}
455
451
}
456
452
457
453
= end code
458
454
459
- In this code we first set up our members, C < $.a_string > and C < $.an_int32 > . We then
460
- set the container of C < $.a_string > to a new C < Str > . After that we declare our
461
- C < init_struct() > function for the C < init() > method to wrap around.
455
+ In this code we first set up our members, C < $.a_string > and
456
+ C < $.an_int32 > . After that we declare our C < init_struct() > function for
457
+ the C < init() > method to wrap around; this function is then called from
458
+ C < BUILD > to effectively assign the values before returning the created
459
+ object.
462
460
463
461
= head3 In your C code...
464
462
@@ -490,13 +488,11 @@ memory that it points <char *a_string> to within the structure as it
490
488
copies the string. (Note you will also have to manage deallocation of
491
489
the memory as well to avoid memory leaks.)
492
490
491
+ = begin code :preamble<class AStringAndAnInt {}>
493
492
# A long time ago in a galaxy far, far away...
494
-
495
- = begin code :skip-test
496
-
497
- my $foo = AStringAndAnInt.new;
498
- $foo.init(a_string => "str", an_int => 123);
499
-
493
+ my $foo = AStringAndAnInt.new(a_string => "str", an_int => 123);
494
+ say "foo is {$foo.a_string} and {$foo.an_int32}";
495
+ # OUTPUT: «foo is str and 123»
500
496
= end code
501
497
502
498
= head1 Typed Pointers
0 commit comments